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

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

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

配置默认的模式

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

  1. public class SchoolContext: DbContext
  2. {
  3. public SchoolDBContext(): base()
  4. {
  5. }
  6. public DbSet<Student> Students { get; set; }
  7. public DbSet<Standard> Standards { get; set; }
  8. protected override void OnModelCreating(DbModelBuilder modelBuilder)
  9. {
  10. //Configure default schema
  11. modelBuilder.HasDefaultSchema("Admin");
  12. }
  13. }
配置实体-->数据表

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

  1. namespace CodeFirst_FluentAPI_Tutorials
  2. {
  3. public class SchoolContext: DbContext
  4. {
  5. public SchoolDBContext(): base()
  6. {
  7. }
  8. public DbSet<Student> Students { get; set; }
  9. public DbSet<Standard> Standards { get; set; }
  10. protected override void OnModelCreating(DbModelBuilder modelBuilder)
  11. {
  12. //Configure default schema
  13. modelBuilder.HasDefaultSchema("Admin");
  14. //Map entity to table
  15. modelBuilder.Entity<Student>().ToTable("StudentInfo");
  16. modelBuilder.Entity<Standard>().ToTable("StandardInfo","dbo");
  17. }
  18. }
  19. }

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

映射实体为多个表

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

  1. namespace CodeFirst_FluentAPI_Tutorials
  2. {
  3. public class SchoolContext: DbContext
  4. {
  5. public SchoolDBContext(): base()
  6. {
  7. }
  8. public DbSet<Student> Students { get; set; }
  9. public DbSet<Standard> Standards { get; set; }
  10. protected override void OnModelCreating(DbModelBuilder modelBuilder)
  11. {
  12. modelBuilder.Entity<Student>().Map(m =>
  13. {
  14. m.Properties(p => new { p.StudentId, p.StudentName});
  15. m.ToTable("StudentInfo");
  16. }).Map(m => {
  17. m.Properties(p => new { p.StudentId, p.Height, p.Weight, p.Photo, p.DateOfBirth});
  18. m.ToTable("StudentInfoDetail");
  19. });
  20. modelBuilder.Entity<Standard>().ToTable("StandardInfo");
  21. }
  22. }
  23. }

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

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

  1. using System.Data.Entity.ModelConfiguration.Configuration;
  2. namespace CodeFirst_FluentAPI_Tutorials
  3. {
  4. public class SchoolContext: DbContext
  5. {
  6. public SchoolDBContext(): base()
  7. {
  8. }
  9. public DbSet<Student> Students { get; set; }
  10. public DbSet<Standard> Standards { get; set; }
  11. protected override void OnModelCreating(DbModelBuilder modelBuilder)
  12. {
  13. modelBuilder.Entity<Student>().Map(delegate(EntityMappingConfiguration<Student> studentConfig)
  14. {
  15. studentConfig.Properties(p => new { p.StudentId, p.StudentName });
  16. studentConfig.ToTable("StudentInfo");
  17. });
  18. Action<EntityMappingConfiguration<Student>> studentMapping = m =>
  19. {
  20. m.Properties(p => new { p.StudentId, p.Height, p.Weight, p.Photo, p.DateOfBirth });
  21. m.ToTable("StudentInfoDetail");
  22. };
  23. modelBuilder.Entity<Student>().Map(studentMapping);
  24. modelBuilder.Entity<Standard>().ToTable("StandardInfo");
  25. }
  26. }
  27. }

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. SpringBoot前端给后端传list

    前端JS "]; var params = { taskList: taskList }; $.ajax({ type: "PUT", dataType: "j ...

  2. Codeforces 679C Bear and Square Grid

    Bear and Square Grid 枚举k * k 的位置, 然后接上它周围白色连通块的数量, 再统计完全在k * k范围里的连通块, 这个只要某个连通块全部的方格 在k * k里面就好, 并且 ...

  3. Docker dockerfile镜像编码

    一. 大多数docker基础镜像使用locale查看编码,发现默认编码都是POSIX,这会导致中文乱码.解决方法如下: 二.首先使用locale -a查看容器所有语言环境 三.dockerfile中加 ...

  4. Kettle学习之Spoon简单使用

    kettle学习之Spoon使用 2018-08-04 10:40:01 首先介绍两个博客入门: https://blog.csdn.net/zzq900503/article/details/785 ...

  5. POJ2065 SETI 高斯消元

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - POJ2065 题意概括 多组数据,首先输入一个T表示数据组数,然后,每次输入一个质数,表示模数,然后,给出一 ...

  6. BZOJ1201 [HNOI2005]数三角形 大力出奇迹

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ1201 题意概括 题解 n3跑过去了,大力出奇迹!简单的,不多说了. 代码 #include < ...

  7. CSS 1. 选择器

    1.css的介绍 CSS是指层叠样式表(Cascading Style Sheets),样式定义如何显示HTML元素,样式通常又会存在于样式表中.也就是说把HTML元素的样式都统一收集起来写在一个地方 ...

  8. Linux开源监控平台 -- Zabbix 小白安装以及使用

    安装准备: 1.安装前需要先关闭selinux和firewall. 关闭Linux: [root@zabbix ~]# vi /etc/selinux/config 将SELINUX=enforcin ...

  9. bzoj 4767: 两双手 组合 容斥

    题目链接 bzoj4767: 两双手 题解 不共线向量构成一组基底 对于每个点\((X,Y)\)构成的向量拆分 也就是对于方程组 $Ax * x + Bx * y = X $ \(Ay * x + B ...

  10. BZOJ 3930: [CQOI2015]选数 莫比乌斯反演

    https://www.lydsy.com/JudgeOnline/problem.php?id=3930 https://blog.csdn.net/ws_yzy/article/details/5 ...