Move Configurations to Separate Class in Code-First:

By now, we have configured all the domain classes in OnModelCreating method in the previous sections. When you have a large number of domain classes, then configuring every class in OnModelCreating can become unmanageable. Code-First enables you to move all the configurations related to one domain class to a separate class.

In the below example, we configured Student entity.

public class SchoolDBContext: DbContext
{
public SchoolDBContext(): base()
{
} public DbSet<Student> Students { get; set; }
public DbSet<Standard> Standards { get; set; }
public DbSet<StudentAddress> StudentAddress { 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");
});
}
}

Now, you can move all the configurations related to Student entity to a separate class which derives from EntityTypeConfiguration<TEntity>. Consider the following StudentEntityConfigurations class.

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");
});
}
}

As you can see above, we have moved all the configuration for the Student entity into constructor of StudentEntityConfiguration, which is derived from EntityTypeConfiguration<Student>. You need to specify entity type in a generic place holder for which you include configurations, Student in this case.

Now, you can inform Fluent API about this class, as shown below.

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

Thus, you can use a separate class to configure a domain class to increase the readability and maintainability.

最新文章

  1. docker学习(6) docker中搭建java服务及nginx反向代理
  2. 继承Thread类
  3. HTML 水平线&lt;hr/&gt;标签
  4. 简单实用的Windows命令(二)
  5. 20145316GDB调试汇编堆栈
  6. 进度条,随机数---demo笔记【原创】
  7. Kafka Topic动态迁移 (源代码解析)
  8. hdu 2822 Dogs(优先队列)
  9. js动态加载控件jsp页面
  10. U盘安装Ubuntu14.4时遇到分区问题记录
  11. bootstrap 基础(二)
  12. WINDOWS SERVER 2016 IE使用FLASH PLAYER的方法
  13. (原)GAN之pix2pix
  14. Java编程的逻辑 (78) - 线程池
  15. linux下依赖库的版本问题引起的安装失败:libssl-dev版本问题无法安装 :libssl-dev : 依赖: libssl1.0.0 (= 1.0.1-4ubuntu3) 但是 1.0.1-4ubuntu5.31 正要被安装
  16. Spring MVC 学习笔记12 —— SpringMVC+Hibernate开发(1)依赖包搭建
  17. lnmp 一键安装
  18. SpringBoot------全局异常捕获
  19. Install Python on Mac
  20. closure--- 闭包与并行运算

热门文章

  1. 红米.USB安装_无法打开
  2. window.showModalDialog()之返回值
  3. Flex学习之(JS中调用Flex的方法)
  4. QFileInfo与QFileIconProvider(分别用于获取文件信息和获取文件的图标)
  5. poj-2420 A Star not a Tree?(模拟退火算法)
  6. 2017人工智能元年,AI在喧嚣和质疑中一路走来
  7. freeMarker(十三)——XML处理指南之揭示XML文档
  8. Chrome focus样式
  9. [HDU5290]Bombing plan
  10. 拓扑排序 POJ 1094 Sorting It All Out