EF 6 Code-First系列文章目录:
- 1 翻译系列:什么是Code First(EF 6 Code First 系列)
- 2.翻译系列:为EF Code-First设置开发环境(EF 6 Code-First系列)
- 3.翻译系列:EF Code-First 示例(EF 6 Code-First系列)
- 4.翻译系列:EF 6 Code-First默认约定(EF 6 Code-First系列)
- 5.翻译系列:EF 6中数据库的初始化(EF 6 Code-First 系列)
- 6.翻译系列:EF 6 Code-First中数据库初始化策略(EF 6 Code-First系列
- 7.翻译系列:EF 6中的继承策略(EF 6 Code-First 系列)
- 8.翻译系列: EF 6中配置领域类(EF 6 Code-First 系列)
- 9.翻译系列:EF 6以及EF Core中的数据注解特性(EF 6 Code-First系列)
- 9.1 翻译系列:数据注解特性之----Table【EF 6 Code-First 系列】
- 9.2 翻译系列:数据注解特性之---Column【EF 6 Code First系列】
- 9.3 翻译系列:数据注解特性之Key【EF 6 Code-First 系列】
- 9.4 翻译系列:EF 6以及 EF Core中的NotMapped特性(EF 6 Code-First系列)
- 9.5 翻译系列:数据注解之ForeignKey特性【EF 6 Code-First系列】
- 9.6 翻译系列:数据注解之Index特性【EF 6 Code-First系列】
- 9.7 翻译系列:EF数据注解特性之--InverseProperty【EF 6 Code-First系列】
- 9.8 翻译系列:数据注解特性之--Required 【EF 6 Code-First系列】
- 9.9 翻译系列:数据注解特性之--MaxLength 【EF 6 Code-First系列】
- 9.10 翻译系列:EF数据注解特性之StringLength【EF 6 Code-First系列】
- 9.11 翻译系列:数据注解特性之--Timestamp【EF 6 Code-First系列】
- 9.12 翻译系列:数据注解特性之ConcurrencyCheck【EF 6 Code-First系列】
- 10.翻译系列:EF 6中的Fluent API配置【EF 6 Code-First系列】
- 10.1.翻译系列:EF 6中的实体映射【EF 6 Code-First系列】
- 10.2.翻译系列:使用Fluent API进行属性映射【EF 6 Code-First】
- 11.翻译系列:在EF 6中配置一对零或者一对一的关系【EF 6 Code-First系列】
- 12.翻译系列:EF 6 中配置一对多的关系【EF 6 Code-First系列】
- 13.翻译系列:Code-First方式配置多对多关系【EF 6 Code-First系列】
- 14.翻译系列:从已经存在的数据库中生成上下文类和实体类【EF 6 Code-First系列】
- 15.翻译系列:EF 6中的级联删除【EF 6 Code-First 系列】
- 16.翻译系列:EF 6 Code -First中使用存储过程【EF 6 Code-First系列】
- 17.翻译系列:将Fluent API的配置迁移到单独的类中【EF 6 Code-First系列】
- 18.翻译系列:EF 6 Code-First 中的Seed Data(种子数据或原始测试数据)【EF 6 Code-First系列】
- 19.翻译系列:EF 6中定义自定义的约定【EF 6 Code-First约定】
- 20.翻译系列:Code-First中的数据库迁移技术【EF 6 Code-First系列】
- 20.1翻译系列:EF 6中自动数据迁移技术【EF 6 Code-First系列】
- 20.2.翻译系列:EF 6中基于代码的数据库迁移技术【EF 6 Code-First系列】
- 21.翻译系列:Entity Framework 6 Power Tools【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}
所以你可以,使用很多的单独的配置类,这样就使得代码,更加可读,更加可维护。
