Entity Framework Code-First(16):Move Configurations
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.
最新文章
- docker学习(6) docker中搭建java服务及nginx反向代理
- 继承Thread类
- HTML 水平线<;hr/>;标签
- 简单实用的Windows命令(二)
- 20145316GDB调试汇编堆栈
- 进度条,随机数---demo笔记【原创】
- Kafka Topic动态迁移 (源代码解析)
- hdu 2822 Dogs(优先队列)
- js动态加载控件jsp页面
- U盘安装Ubuntu14.4时遇到分区问题记录
- bootstrap 基础(二)
- WINDOWS SERVER 2016 IE使用FLASH PLAYER的方法
- (原)GAN之pix2pix
- Java编程的逻辑 (78) - 线程池
- linux下依赖库的版本问题引起的安装失败:libssl-dev版本问题无法安装 :libssl-dev : 依赖: libssl1.0.0 (= 1.0.1-4ubuntu3) 但是 1.0.1-4ubuntu5.31 正要被安装
- Spring MVC 学习笔记12 —— SpringMVC+Hibernate开发(1)依赖包搭建
- lnmp 一键安装
- SpringBoot------全局异常捕获
- Install Python on Mac
- closure--- 闭包与并行运算
热门文章
- 红米.USB安装_无法打开
- window.showModalDialog()之返回值
- Flex学习之(JS中调用Flex的方法)
- QFileInfo与QFileIconProvider(分别用于获取文件信息和获取文件的图标)
- poj-2420 A Star not a Tree?(模拟退火算法)
- 2017人工智能元年,AI在喧嚣和质疑中一路走来
- freeMarker(十三)——XML处理指南之揭示XML文档
- Chrome focus样式
- [HDU5290]Bombing plan
- 拓扑排序 POJ 1094 Sorting It All Out