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.
Entity Framework Code-First(16):Move Configurations的更多相关文章
- Entity Framework Tutorial Basics(16):Linq-to-Entities Projection Queries
Linq-to-Entities Projection Queries: Here, you will learn how to write LINQ-to-Entities queries and ...
- Entity Framework Code first(转载)
一.Entity Framework Code first(代码优先)使用过程 1.1Entity Framework 代码优先简介 不得不提Entity Framework Code First这个 ...
- Entity Framework Code First (三)Data Annotations
Entity Framework Code First 利用一种被称为约定(Conventions)优于配置(Configuration)的编程模式允许你使用自己的 domain classes 来表 ...
- Entity Framework Code First (二)Custom Conventions
---------------------------------------------------------------------------------------------------- ...
- Entity Framework Code First (一)Conventions
Entity Framework 简言之就是一个ORM(Object-Relational Mapper)框架. Code First 使得你能够通过C#的类来描述一个模型,模型如何被发现/检测就是通 ...
- Entity Framework Tutorial Basics(11):Code First
Code First development with Entity Framework: Entity Framework supports three different development ...
- Entity Framework Code First (七)空间数据类型 Spatial Data Types
声明:本文针对 EF5+, Visual Studio 2012+ 空间数据类型(Spatial Data Types)是在 EF5 中引入的,空间数据类型表现有两种: Geography (地理学上 ...
- Entity Framework Code First (四)Fluent API - 配置属性/类型
上篇博文说过当我们定义的类不能遵循约定(Conventions)的时候,Code First 提供了两种方式来配置你的类:DataAnnotations 和 Fluent API, 本文将关注 Flu ...
- Entity Framework Code First (八)迁移 Migrations
创建初始模型和数据库 在开始使用迁移(Migrations)之前,我们需要一个 Project 和一个 Code First Model, 对于本文将使用典型的 Blog 和 Post 模型 创建一个 ...
随机推荐
- WCF REST开启Cors 解决 No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access. The response had HTTP status code 405.
现象: 编写了REST接口: [ServiceContract] public interface IService1 { [OperationContract] [WebInvoke(UriTemp ...
- Win7.窗口自动滚动回到屏幕
PS:笔记本 ThinkPad E440 1.前提:窗口 的一部分位于在屏幕的外面,此窗口处于激活的状态 操作:鼠标放置在窗口内部,鼠标继续往屏幕外部移动,鼠标没有任何其他事件(只有 MouseMov ...
- Spark- 计算每个学科最受欢迎的老师
日志类型 测试数据 http://bigdata.myit.com/zhangsan http://bigdata.myit.com/zhangsan http://bigdata.myit.com/ ...
- yield生成器的经典案例
如何生成斐波那契數列 斐波那契(Fibonacci)數列是一个非常简单的递归数列,除第一个和第二个数外,任意一个数都可由前两个数相加得到.用计算机程序输出斐波那契數列的前 N 个数是一个非常简单的问题 ...
- C++(六)— 输入方式
1.输入包含空格的字符串 使用 getline(cin, str)读取一行字符串,遇到换行符停止:cin>>str,是遇到空格就停止. 实现:输入两个字符,在第一个字符中删除第二个字符中出 ...
- Python--基础文件读写操作
1,open(),对文件进行读写操作之前,要先打开文件,获取文件的句柄: 懒人专用方法,文件打开后不用关闭 with open(r'somefile.txt','r+',encoding='utf8' ...
- PHP之冒泡排序的优化
冒泡排序是一个常见的排序算法,它重复地走访过要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换过来.走访数列的工作是重复地进行直到没有再需要交换,也就是说该数列已经排序完成. 对于冒泡排序 ...
- 201621123014《JAVA程序设计》第1周学习总结
1. 本章学习总结 关键字:JAVA特点.JDK.JVM.JRE.class.编译工具.JDK是JAVA的开发工具包,拥有JAVA需要的环境和各类JAVA工具,是JAVA的核心:JVM是JAVA虚拟机 ...
- L106 Three things we learned from day one at the World Cup
Hosts Russia got the World Cup off to a flying start by hammering Saudi Arabia 5-0 in the opening ga ...
- HasnMap的一种遍历方式:Map.Entry 和 Map.entrySet()
1.Map.Entry 和 Map.entrySet()分别是什么? Map.entrySet():根据名字便可知道,这是一个集合,是一个映射项的set. Map.Entry<k,v>: ...