17.翻译系列:将Fluent API的配置迁移到单独的类中【EF 6 Code

原文链接: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实体中配置的代码:

1public class SchoolDBContext: DbContext 2{ 3 public SchoolDBContext(): base() 4 { 5 } 6 7 public DbSet<Student> Students { get; set; } 8 9 protected override void OnModelCreating(DbModelBuilder modelBuilder) 10 { 11 modelBuilder.Entity<Student>().ToTable("StudentInfo"); 12 13 modelBuilder.Entity<Student>().HasKey<int>(s => s.StudentKey); 14 15 modelBuilder.Entity<Student>() 16 .Property(p => p.DateOfBirth) 17 .HasColumnName("DoB") 18 .HasColumnOrder(3) 19 .HasColumnType("datetime2"); 20 21 modelBuilder.Entity<Student>() 22 .Property(p => p.StudentName) 23 .HasMaxLength(50); 24 25 modelBuilder.Entity<Student>() 26 .Property(p => p.StudentName) 27 .IsConcurrencyToken(); 28 29 modelBuilder.Entity<Student>() 30 .HasMany<Course>(s => s.Courses) 31 .WithMany(c => c.Students) 32 .Map(cs => 33 { 34 cs.MapLeftKey("StudentId"); 35 cs.MapRightKey("CourseId"); 36 cs.ToTable("StudentCourse"); 37 }); 38 } 39}

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

1public class StudentEntityConfiguration: EntityTypeConfiguration<Student> 2{ 3 public StudentEntityConfiguration() 4 { 5 this.ToTable("StudentInfo"); 6 7 this.HasKey<int>(s => s.StudentKey); 8 9 this.Property(p => p.DateOfBirth) 10 .HasColumnName("DoB") 11 .HasColumnOrder(3) 12 .HasColumnType("datetime2"); 13 14 this.Property(p => p.StudentName) 15 .HasMaxLength(50); 16 17 this.Property(p => p.StudentName) 18 .IsConcurrencyToken(); 19 20 this.HasMany<Course>(s => s.Courses) 21 .WithMany(c => c.Students) 22 .Map(cs => 23 { 24 cs.MapLeftKey("StudentId"); 25 cs.MapRightKey("CourseId"); 26 cs.ToTable("StudentCourse"); 27 }); 28 } 29}

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

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

1public class SchoolDBContext: DbContext 2{ 3 public SchoolDBContext(): base() 4 { 5 } 6 7 public DbSet<Student> Students { get; set; } 8 9 protected override void OnModelCreating(DbModelBuilder modelBuilder) 10 { 11 // Moved all Student related configuration to StudentEntityConfiguration class 12 modelBuilder.Configurations.Add(new StudentEntityConfiguration()); 13 } 14}

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

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )