原文链接:https://www.entityframeworktutorial.net/code-first/move-configurations-to-seperate-class-in-code-first.aspx

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

你在前面的章节中已经看到,我们在OnModelCreating()方法中,使用Fluent API为所有的实体类进行配置。然而这样做,当配置的实体类很多的时候,就变得很难维护了。EF 6 允许你单独为每个实体,在单独的类中进行配置。

看看下面我们在Student实体中配置的代码:

public class SchoolDBContext: DbContext
{
public SchoolDBContext(): base()
{
} public DbSet<Student> Students { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Student>().ToTable("StudentInfo"); modelBuilder.Entity<Student>().HasKey<int>(s => s.StudentKey); modelBuilder.Entity<Student>()
.Property(p => p.DateOfBirth)
.HasColumnName("DoB")
.HasColumnOrder()
.HasColumnType("datetime2"); modelBuilder.Entity<Student>()
.Property(p => p.StudentName)
.HasMaxLength(); modelBuilder.Entity<Student>()
.Property(p => p.StudentName)
.IsConcurrencyToken(); modelBuilder.Entity<Student>()
.HasMany<Course>(s => s.Courses)
.WithMany(c => c.Students)
.Map(cs =>
{
cs.MapLeftKey("StudentId");
cs.MapRightKey("CourseId");
cs.ToTable("StudentCourse");
});
}
}

你可以将上面的配置部分,移动到单独的类中,这个类继承自EntityTypeConfiguration<TEntity>。看看下面的StudentEntityConfigurations类,它包含所有Student类的配置:

public class StudentEntityConfiguration: EntityTypeConfiguration<Student>
{
public StudentEntityConfiguration()
{
this.ToTable("StudentInfo"); this.HasKey<int>(s => s.StudentKey); this.Property(p => p.DateOfBirth)
.HasColumnName("DoB")
.HasColumnOrder()
.HasColumnType("datetime2"); this.Property(p => p.StudentName)
.HasMaxLength(); this.Property(p => p.StudentName)
.IsConcurrencyToken(); this.HasMany<Course>(s => s.Courses)
.WithMany(c => c.Students)
.Map(cs =>
{
cs.MapLeftKey("StudentId");
cs.MapRightKey("CourseId");
cs.ToTable("StudentCourse");
});
}
}

正如上面的代码你所看到的那样,我们将所有Student的配置部分,放到了StudentEntityConfiguration类的构造函数中。

现在你需要使用Fluent API添加这个配置类:

public class SchoolDBContext: DbContext
{
public SchoolDBContext(): base()
{
} public DbSet<Student> Students { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// Moved all Student related configuration to StudentEntityConfiguration class
modelBuilder.Configurations.Add(new StudentEntityConfiguration());
}
}

所以你可以,使用很多的单独的配置类,这样就使得代码,更加可读,更加可维护。

17.翻译系列:将Fluent API的配置迁移到单独的类中【EF 6 Code-First系列】的更多相关文章

  1. 9.2 翻译系列:数据注解特性之---Column【EF 6 Code First系列】

    原文链接:http://www.entityframeworktutorial.net/code-first/column-dataannotations-attribute-in-code-firs ...

  2. 1 翻译系列:什么是Code First(EF 6 Code First 系列)

    原文链接:http://www.entityframeworktutorial.net/code-first/what-is-code-first.aspx EF 6 Code-First系列文章目录 ...

  3. 一起学ASP.NET Core 2.0学习笔记(二): ef core2.0 及mysql provider 、Fluent API相关配置及迁移

    不得不说微软的技术迭代还是很快的,上了微软的船就得跟着她走下去,前文一起学ASP.NET Core 2.0学习笔记(一): CentOS下 .net core2 sdk nginx.superviso ...

  4. Spring boot将配置属性注入到bean类中

    一.@ConfigurationProperties注解的使用 看配置文件,我的是yaml格式的配置: // file application.yml my: servers: - dev.bar.c ...

  5. 【翻译】Flink Table Api & SQL — 配置

    本文翻译自官网:Configuration https://ci.apache.org/projects/flink/flink-docs-release-1.9/dev/table/config.h ...

  6. EF Code First教程-02.1 Fluent API约定配置

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...

  7. EF fluent API如何配置主键不自动增长

    在Dbcontext中作如下添加: protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilde ...

  8. 【Java面试题】17 如何把一个逗号分隔的字符串转换为数组? 关于String类中split方法的使用,超级详细!!!

    split 方法:将一个字符串分割为子字符串,然后将结果作为字符串数组返回. stringObj.split([separator],[limit])参数:stringObj   必选项.要被分解的 ...

  9. 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 ...

随机推荐

  1. HDU 1540 Tunnel Warfare(经典)(区间合并)【线段树】

    <题目链接> 题目大意: 一个长度为n的线段,下面m个操作 D x 表示将单元x毁掉 R  表示修复最后毁坏的那个单元 Q x  询问这个单元以及它周围有多少个连续的单元,如果它本身已经被 ...

  2. Git 日常工作中使用的命令记录

    前言   这篇文章主要是介绍我在使用Git中的有一些忘记了,但是很重要的命令. 20190424 Git 历史信息 username 和 email 更改 git config alias.chang ...

  3. 上海市2019年公务员录用考试笔试合格人员笔试成绩(B类)

    考试类别:B类 注册编号 总成绩 注册编号 总成绩 注册编号 总成绩 注册编号 总成绩 5101919 154.7 5113380 156.5 5126791 133.5 5146138 126.2 ...

  4. XamarinEssentials教程移除键值首选项的键值

    XamarinEssentials教程移除键值首选项的键值 如果开发者不再使用首选项中的某一项时,可以将该项移除掉.此时可以使用Preferences类的Remove()方法,该方法可以将存在于首选项 ...

  5. react组件通信那些事儿

    父组件是调用组件的组件.现在看来,感觉父组件就是一个壳子,定义好壳子里面会有什么,而子组件是一个具体的实现,说明,会用到什么东西,如果有这些东西,会进行什么操作.总之,父组件是材料,有水和泥,子组件告 ...

  6. [OC] @property时,copy、strong、weak、assign的区别

    @property(copy,nonatomic)NSMutableString*copyStr; @property(strong,nonatomic)NSMutableString*strongS ...

  7. 在UnrealEngine中用Custom节点实现马赛克效果

    参考这位大神的Shaderhttp://blog.csdn.net/noahzuo/article/details/51316015 //input BaseUV 屏幕UV //intput Tili ...

  8. 【ACM】 1231 最大连续子序列

    [1231 最大连续子序列 ** Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...

  9. python基础一 ------Python 的编码

    首先了解一下历史,但是本篇文章冗杂,如老太太裹脚布----------又臭又长 编码历史: 1. 计算机只能处理数字,文本文件只有转换为数字    才能处理.8bit==1字节 所以一个字节能表示的最 ...

  10. [三边定位] C# 演示程序

    计划用CC2530做定位,网上找了一些求圆交点的程序, 修改成3个圆求交点的质心,感觉算法还行. 粗略写了一下程序,结果还行. 现在只能手动输入3个圆的信息. 后面需要再优化. 全部未优化的程序: u ...