原文链接: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. 002.Zabbix简介

    一 Zabbix简介 1.1 概述 Zabbix是一个企业级的高度集成开源监控软件,提供分布式监控解决方案.可以用来监控设备.服务等可用性和性能. 1.2 所支持监控方式 目前由zabbix提供包括但 ...

  2. Django基础(一)

    Django基础(一) 知识预览 Django基本命令 二 路由配置系统(URLconf) 三 编写视图 四 Template 五 数据库与ORM admin的配置 一 什么是web框架? 框架,即f ...

  3. pyquery 库的方法

    初始化 在这里介绍四种初始化方式. (1)直接字符串 from pyquery import PyQuery as pq doc = pq("<html></html> ...

  4. SQL总结——存储过程

    SQL总结(五)存储过程 概念 存储过程(Stored Procedure):已预编译为一个可执行过程的一个或多个SQL语句. 创建存储过程语法 CREATE proc | procedure pro ...

  5. spring boot + jpa + bootstrap + thymeleaf 简单的增删改查Demo

    对springboot和bootstrap初学者来说是一个不错Demo 下载地址:点击进入下载Demo 首页(http://localhost:8081) 增加 编辑 搜索

  6. BZOJ.4340.[BJOI2015]隐身术(后缀数组 搜索)

    BZOJ \(Description\) 给定两个串\(S,T\)以及一个数\(k\),求\(T\)中有多少个子串,满足和\(S\)的编辑距离不超过\(k\). \(|S|+|T|\leq10^5,\ ...

  7. PAT Basic 1007

    1007 素数对猜想 (20 分) 让我们定义d​n​​为:d​n​​=p​n+1​​−p​n​​,其中p​i​​是第i个素数.显然有d​1​​=1,且对于n>1有d​n​​是偶数.“素数对猜想 ...

  8. Python核心编程(第二版)正则表达式练习题解

    15-1. 识别下列字符串:“bat,” “bit,” “but,” “hat,” “hit,” 或 “hut” from re import match word = raw_input('inpu ...

  9. IO流(2)—知识结构

    结构: 注:此IO包下主要介绍: 节点流:(字节流)FileInputStream.FileOutputStream.(字符流)Filereader.FileWriter 处理流(缓冲流):(字节流) ...

  10. What is the NETStandard.Library metapackage?

    In my last post, I took a quick look at the Microsoft.AspNetCore meta package. One of the libraries ...