原文链接:https://www.entityframeworktutorial.net/code-first/move-configurations-to-seperate-class-in-code-first.aspx

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

你可以将上面的配置部分,移动到单独的类中,这个类继承自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()
.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");
});
}
}

正如上面的代码你所看到的那样,我们将所有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());
}
}

所以你可以,使用很多的单独的配置类,这样就使得代码,更加可读,更加可维护。

17.翻译系列:将Fluent API的配置迁移到单独的类中【EF 6 Code-First系列】的更多相关文章

  1. 9.2 翻译系列:数据注解特性之---Column【EF 6 Code First系列】

    原文链接:http://www.entityframeworktutorial.net/code-first/column-dataannotations-attribute-in-code-firs ...

  2. 1 翻译系列:什么是Code First(EF 6 Code First 系列)

    原文链接:http://www.entityframeworktutorial.net/code-first/what-is-code-first.aspx EF 6 Code-First系列文章目录 ...

  3. 一起学ASP.NET Core 2.0学习笔记(二): ef core2.0 及mysql provider 、Fluent API相关配置及迁移

    不得不说微软的技术迭代还是很快的,上了微软的船就得跟着她走下去,前文一起学ASP.NET Core 2.0学习笔记(一): CentOS下 .net core2 sdk nginx.superviso ...

  4. Spring boot将配置属性注入到bean类中

    一.@ConfigurationProperties注解的使用 看配置文件,我的是yaml格式的配置: // file application.yml my: servers: - dev.bar.c ...

  5. 【翻译】Flink Table Api & SQL — 配置

    本文翻译自官网:Configuration https://ci.apache.org/projects/flink/flink-docs-release-1.9/dev/table/config.h ...

  6. EF Code First教程-02.1 Fluent API约定配置

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...

  7. EF fluent API如何配置主键不自动增长

    在Dbcontext中作如下添加: protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilde ...

  8. 【Java面试题】17 如何把一个逗号分隔的字符串转换为数组? 关于String类中split方法的使用,超级详细!!!

    split 方法:将一个字符串分割为子字符串,然后将结果作为字符串数组返回. stringObj.split([separator],[limit])参数:stringObj   必选项.要被分解的 ...

  9. 10.翻译系列:EF 6中的Fluent API配置【EF 6 Code-First系列】

    原文链接:https://www.entityframeworktutorial.net/code-first/fluent-api-in-code-first.aspx EF 6 Code-Firs ...

随机推荐

  1. HDU3339 In Action 【最短路】+【01背包】

    <题目链接> 题目大意: 给出一个0-n组成的图,1-n的点上分布着值为pow的电站,给出图的m条边以及距离,从0出发到n个点中的x个点的行走距离和最小(因为是每炸一个点派出一辆坦克),且 ...

  2. docker eureka 注册中心 服务提供者通信机制

    docker run -d --name gateway9527 -p 9527:9527  --add-host=eureka7001.com:192.168.1.15    -v /var/jen ...

  3. Python中list、字典、字符串的讲解

    python 的list讲解  计算机中的数组是从0开始的 list中的下标.角标.索引说的都是一个 数组都是从0开始的. stus=["刘",“王”,“张”] stus2=[] ...

  4. 每月学习数理统计--《统计学习方法—李航》(3): SVM

    1. SVM的最优化问题 2.拉格朗日乘数法,对偶条件KKT条件 3.软件隔支持向量机 4.非线性支持向量机,核函数 5.SMO算法 1. SVM的最优化问题 支持向量机(Support Vector ...

  5. C# SQLiteHelper

    using System; using System.Data; using System.Data.Common; using System.Data.SQLite; using System.IO ...

  6. C# 控制左右声道

    private void button1_Click(object sender, EventArgs e) { axWindowsMediaPlayer1.settings.balance = -; ...

  7. JS实现缓动动画效果

    原理如下: 假设要从数值A变化到数值B,如果是线性运动,则每次移动距离是一样:如果是缓动,每次移动距离不一样.那如何才能不一样呢?很简单,按比例移动就可以. 例如:每次移动剩余距离的一半. 对吧,超容 ...

  8. [CC-INVENTRY]Arranging the Inventory

    [CC-INVENTRY]Arranging the Inventory 题目大意: 有一排长度为\(n(\sum n\le10^6)\)的格子,有些格子是空的,有些格子上有一个箱子. 现在你要用最小 ...

  9. python网络编程(七)

    应用:模拟QQ聊天 客户端参考代码 #coding=utf-8 from socket import * # 创建socket tcpClientSocket = socket(AF_INET, SO ...

  10. 【NOI2015】【BZOJ4196】软件包管理器 - 题解

    Description Linux用户和OSX用户一定对软件包管理器不会陌生.通过软件包管理器,你可以通过一行命令安装某一个软件包,然后软件包管理器会帮助你从软件源下载软件包,同时自动解决所有的依赖( ...