17.翻译系列:将Fluent API的配置迁移到单独的类中【EF 6 Code-First系列】
2019-04-13 22:02
1021 查看
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实体中配置的代码:
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(3) .HasColumnType("datetime2"); modelBuilder.Entity<Student>() .Property(p => p.StudentName) .HasMaxLength(50); 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(3) .HasColumnType("datetime2"); this.Property(p => p.StudentName) .HasMaxLength(50); 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()); } }
所以你可以,使用很多的单独的配置类,这样就使得代码,更加可读,更加可维护。
相关文章推荐
- 20.翻译系列:Code-First中的数据库迁移技术【EF 6 Code-First系列】
- 20.1翻译系列:EF 6中自动数据迁移技术【EF 6 Code-First系列】
- 20.2.翻译系列:EF 6中基于代码的数据库迁移技术【EF 6 Code-First系列】
- Entity Framework Code First (五)Fluent API - 配置关系
- .net core 2.0 Code First Fluent API配置
- Entity Framework Code First (四)Fluent API - 配置属性/类型
- EntityFramework Code-First 简易教程(七)-------领域类配置之Fluent API
- Entity Framework Code First (五)Fluent API - 配置关系
- EF框架step by step(9)—Code First Fluent API
- Entity Framework Code First (五)Fluent API - 配置关系
- Entity Framework Code First (四)Fluent API - 配置属性/类型
- 一起学ASP.NET Core 2.0学习笔记(二)- ef core2.0 及mysql provider 、Fluent API相关配置及迁移
- 18.翻译系列:EF 6 Code-First 中的Seed Data(种子数据或原始测试数据)【EF 6 Code-First系列】
- EF框架step by step(9)—Code First Fluent API
- 一起学ASP.NET Core 2.0学习笔记(二): ef core2.0 及mysql provider 、Fluent API相关配置及迁移
- Entity Framework Code First (四)Fluent API - 配置属性/类型
- Entity Framework Code First (五)Fluent API - 配置关系
- 19.翻译系列:EF 6中定义自定义的约定【EF 6 Code-First约定】
- Fluent API in Code-First【Code-First系列】
- 【EF】EF框架 Code First Fluent API