原文链接:https://www.entityframeworktutorial.net/code-first/configure-entity-mappings-using-fluent-api.aspx

EF 6 Code-First系列文章目录:

Fluent API可以配置实体,为其映射为数据表,默认的模式等。

配置默认的模式

首先,让我们来配置数据库中数据表的默认的模式名吧。当然你可以在配置单独的表的时候,改变这个默认的模式。下面的代码设置默认的模式名为Admin,所有的数据库对象都将会是你配置的这个模式名。

public class SchoolContext: DbContext
{
public SchoolDBContext(): base()
{
} public DbSet<Student> Students { get; set; }
public DbSet<Standard> Standards { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
//Configure default schema
modelBuilder.HasDefaultSchema("Admin");
}
}
配置实体-->数据表

Code-First将会以上下文类中的DbSet类型的属性,创建数据表。在这个例子中是Students和Standards表。你可以单独指定表名,以重写DBset类型的属性所生成的表,例如:

namespace CodeFirst_FluentAPI_Tutorials
{
public class SchoolContext: DbContext
{
public SchoolDBContext(): base()
{
} public DbSet<Student> Students { get; set; }
public DbSet<Standard> Standards { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
//Configure default schema
modelBuilder.HasDefaultSchema("Admin"); //Map entity to table
modelBuilder.Entity<Student>().ToTable("StudentInfo");
modelBuilder.Entity<Standard>().ToTable("StandardInfo","dbo");
}
}
}

正如上面代码所见,我们配置以Entity()方法开始,大多数的时候,使用Fluent API,你必须要以这个方法开始。我们使用ToTable()方法,将Student实体映射为StudentInfo表,将Standard实体映射为StandardInfo表,并且单独配置了StandardInfo表的模式名为dbo.

映射实体为多个表

下面的例子,演示了将Student实体,映射为多个表。

namespace CodeFirst_FluentAPI_Tutorials
{
public class SchoolContext: DbContext
{
public SchoolDBContext(): base()
{
} public DbSet<Student> Students { get; set; }
public DbSet<Standard> Standards { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Student>().Map(m =>
{
m.Properties(p => new { p.StudentId, p.StudentName});
m.ToTable("StudentInfo");
}).Map(m => {
m.Properties(p => new { p.StudentId, p.Height, p.Weight, p.Photo, p.DateOfBirth});
m.ToTable("StudentInfoDetail");
}); modelBuilder.Entity<Standard>().ToTable("StandardInfo");
}
}
}

正如上面代码所示,我们将Student实体的一些属性映射为StudentInfo表,使用Map方法将Student实体的另外一些属性映射为StudentInfoDetail表,所以Student实体将会拆分为两个数据表:

Map方法需要一个委托类型的参数,你可以在Man方法中传递Action委托或者lambda表达式,例如:

using System.Data.Entity.ModelConfiguration.Configuration;

namespace CodeFirst_FluentAPI_Tutorials
{
public class SchoolContext: DbContext
{
public SchoolDBContext(): base()
{
} public DbSet<Student> Students { get; set; }
public DbSet<Standard> Standards { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Student>().Map(delegate(EntityMappingConfiguration<Student> studentConfig)
{
studentConfig.Properties(p => new { p.StudentId, p.StudentName });
studentConfig.ToTable("StudentInfo");
}); Action<EntityMappingConfiguration<Student>> studentMapping = m =>
{
m.Properties(p => new { p.StudentId, p.Height, p.Weight, p.Photo, p.DateOfBirth });
m.ToTable("StudentInfoDetail");
}; modelBuilder.Entity<Student>().Map(studentMapping);
modelBuilder.Entity<Standard>().ToTable("StandardInfo");
}
}
}

10.1.翻译系列:EF 6中的实体映射【EF 6 Code-First系列】的更多相关文章

  1. 【mybatis深度历险系列】mybatis中的高级映射一对一、一对多、多对多

    学习hibernate的时候,小编已经接触多各种映射,mybatis中映射有到底是如何运转的,今天这篇博文,小编主要来简单的介绍一下mybatis中的高级映射,包括一对一.一对多.多对多,希望多有需要 ...

  2. 【mybatis深度历险系列】mybatis中的输入映射和输出映射

    在前面的博文中,小编介绍了mybatis的框架原理以及入门程序,还有mybatis中开发到的两种方法,原始开发dao的方法和mapper代理方法,今天博文,我们来继续学习mybatis中的相关知识,随 ...

  3. ef linq 中判断实体中是否包含某集合

    我有一个需求,问题有很多标签,在查询时,需要筛选包含查询标签的一个集合(List<int>),以前的做法是先查询出来符合查询标签条件的标签id的结果集A,再查询问题时,加上判断是否包含该标 ...

  4. 在 EF Core 中 Book 实体在新增、修改、删除时,给 LastUpdated 字段赋值。

    直接贴代码: public class MenusContext : DbContext { public static class ColumnNames { public const string ...

  5. Hibernate中的实体映射

     一.一对一映射  如人(Person)与身份证(IdCard) 的关系,即为一对一的关系,一个人只能有一张身份证,一张身份证只能属于某一个人,它们的关系图如下图所示: 在Person实体中添加一个属 ...

  6. EF架构~对AutoMapper实体映射的扩展

    回到目录 AutoMapper在之前我曾经介绍过,今天主要是把它作一下扩展,因为它的调用太麻烦了,呵呵,扩展之后,用着还可以,感觉.net3.5之后,有了扩展方法这个东西,在程序开发速度及表现力上都有 ...

  7. 10.翻译系列:EF 6中的Fluent API配置【EF 6 Code-First系列】

    原文链接:https://www.entityframeworktutorial.net/code-first/fluent-api-in-code-first.aspx EF 6 Code-Firs ...

  8. 20.2.翻译系列:EF 6中基于代码的数据库迁移技术【EF 6 Code-First系列】

    原文链接:https://www.entityframeworktutorial.net/code-first/code-based-migration-in-code-first.aspx EF 6 ...

  9. 20.1翻译系列:EF 6中自动数据迁移技术【EF 6 Code-First系列】

    原文链接:https://www.entityframeworktutorial.net/code-first/automated-migration-in-code-first.aspx EF 6 ...

随机推荐

  1. 094实战 关于js SDK的程序,java SDK的程序

    一:JS SDK 1.修改配置workspace 2.导入 3.Demo.html <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Trans ...

  2. ASP.NET Core 文件系统

    ASP.NET Core 文件系统 静态文件 目录浏览 默认页面 MIME类型配置 实战文件服务器  紧接上一讲 中间件 之后,今天来我们来讲一下关于 ASP.NET  Core 中静态文件服务. 什 ...

  3. SpringMVC框架09——@ResponseBody的用法详解

    @ResponseBody可以标注在方法上也可以标注在类上面.简单来说,当标注在方法上时,该方法的返回结果直接转成JSON格式:当标注在类上时,该类中的所有方法的返回结果都转换成JSON格式. 代码示 ...

  4. atomic 包、synchronized | Java 中线程安全

    相关阅读 彻底搞懂 CPU 中的内存结构 Java 内存模型 ,一篇就够了! 多线程实现原理 之前已经说过了,我们在保证线程安全的时候主要就是保证线程安全的 3 大特性,原子性.可见性.有序性.而在 ...

  5. js算法初窥03(搜索及去重算法)

    前面我们了解了一些常用的排序算法,那么这篇文章我们来看看搜索算法的一些简单实现,我们先来介绍一个我们在实际工作中一定用到过的搜索算法——顺序搜索. 1.顺序搜索 其实顺序搜索十分简单,我们还是以第一篇 ...

  6. java 三目运算符

    三目运算符 可以内嵌使用. level=(input>90)?"Class A":(input>60)?"Class B":"Class ...

  7. Web大前端面试题-Day8

    1. 说说你对作用域链的理解? 作用域链的作用是保证执行环境里 有权访问的变量和函数是有序的, 作用域链的变量只能向上访问, 变量访问到window对象即被终止, 作用域链向下访问变量是不被允许的; ...

  8. 2041 ACM 超级楼梯

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=2041 数学问题,找规律,可以先假设全一步,然后一个两步的,两个两步的~~.很容易发现规律:F[N]=F[N ...

  9. c潭州课堂25班:Ph201805201 MySQL第二课 (课堂笔记)

    mysql> create table tb_2( -> id int, -> name varchar(10) not null -> ); 插入数据 insert into ...

  10. [BZOJ2650]积木

    [BZOJ2650]积木 题目大意: 有一排\(n\)个积木,第\(i\)个积木的高度为\(h_i\),定义混乱值为相邻两个积木高度之差的绝对值之和乘上系数\(c\).可以花费\(t^2\)的代价将一 ...