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

2022-11-02,,,,

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

EF 6 Code-First系列文章目录:

1 翻译系列:什么是Code First(EF 6 Code First 系列)
2.翻译系列:为EF Code-First设置开发环境(EF 6 Code-First系列)
3.翻译系列:EF Code-First 示例(EF 6 Code-First系列)
4.翻译系列:EF 6 Code-First默认约定(EF 6 Code-First系列)
5.翻译系列:EF 6中数据库的初始化(EF 6 Code-First 系列)
6.翻译系列:EF 6 Code-First中数据库初始化策略(EF 6 Code-First系列
7.翻译系列:EF 6中的继承策略(EF 6 Code-First 系列)
8.翻译系列: EF 6中配置领域类(EF 6 Code-First 系列)
9.翻译系列:EF 6以及EF Core中的数据注解特性(EF 6 Code-First系列)
9.1 翻译系列:数据注解特性之----Table【EF 6 Code-First 系列】
9.2 翻译系列:数据注解特性之---Column【EF 6 Code First系列】
9.3 翻译系列:数据注解特性之Key【EF 6 Code-First 系列】
9.4 翻译系列:EF 6以及 EF Core中的NotMapped特性(EF 6 Code-First系列)
9.5 翻译系列:数据注解之ForeignKey特性【EF 6 Code-First系列】
9.6 翻译系列:数据注解之Index特性【EF 6 Code-First系列】
9.7 翻译系列:EF数据注解特性之--InverseProperty【EF 6 Code-First系列】
9.8 翻译系列:数据注解特性之--Required 【EF 6 Code-First系列】
9.9 翻译系列:数据注解特性之--MaxLength 【EF 6 Code-First系列】
9.10 翻译系列:EF数据注解特性之StringLength【EF 6 Code-First系列】
9.11 翻译系列:数据注解特性之--Timestamp【EF 6 Code-First系列】
9.12 翻译系列:数据注解特性之ConcurrencyCheck【EF 6 Code-First系列】
10.翻译系列:EF 6中的Fluent API配置【EF 6 Code-First系列】
10.1.翻译系列:EF 6中的实体映射【EF 6 Code-First系列】
10.2.翻译系列:使用Fluent API进行属性映射【EF 6 Code-First】
11.翻译系列:在EF 6中配置一对零或者一对一的关系【EF 6 Code-First系列】
12.翻译系列:EF 6 中配置一对多的关系【EF 6 Code-First系列】
13.翻译系列:Code-First方式配置多对多关系【EF 6 Code-First系列】
14.翻译系列:从已经存在的数据库中生成上下文类和实体类【EF 6 Code-First系列】
15.翻译系列:EF 6中的级联删除【EF 6 Code-First 系列】
16.翻译系列:EF 6 Code -First中使用存储过程【EF 6 Code-First系列】
17.翻译系列:将Fluent API的配置迁移到单独的类中【EF 6 Code-First系列】
18.翻译系列:EF 6 Code-First 中的Seed Data(种子数据或原始测试数据)【EF 6 Code-First系列】
19.翻译系列:EF 6中定义自定义的约定【EF 6 Code-First约定】
20.翻译系列:Code-First中的数据库迁移技术【EF 6 Code-First系列】
20.1翻译系列:EF 6中自动数据迁移技术【EF 6 Code-First系列】
20.2.翻译系列:EF 6中基于代码的数据库迁移技术【EF 6 Code-First系列】
21.翻译系列:Entity Framework 6 Power Tools【EF 6 Code-First系列】

Entity Framework Fluent API是用来配置领域类,以重写默认约定的。EF Fluent API是基于Fluent API设计模式的(也就是Fluent接口),Fluent API是通过方法链式调用来配置实体的。
在EF 6中,DBModelBuilder类就充当Fluent API,我们可以使用它来配置很多东西。Fluent API提供了比数据注解更多的配置选项。
为了使用Fluent API的配置,我们需要在上下文类中,重写DbContext类的OnModelCreating方法,例如:

public class SchoolContext: DbContext
{ public DbSet<Student> Students { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
//Write Fluent API configurations here }
}

你可以同时使用数据注解特性和Fluent API。Entity Framework优先使用Fluent API的配置,而不是数据注解。
在EF 6中,Fluent API配置模型的以下方面:

    Model-wide Configuration:配置默认的Schema、映射的时候排除实体等等。
    Entity Configuration:将实体配置成数据表以及关系的映射。例如:主键、索引、数据表的名称、一对一关系、一对多关系、多对多关系等。
    Property Configuration:将属性配置成数据列。例如:列的名称、列是否可空、外键、列的数据类型、并发列等等。

下面的图形列出来了Fluent API重要的方法。

后面的章节中,我们开始学习使用Fluent API配置实体吧。

10.翻译系列:EF 6中的Fluent API配置【EF 6 Code-First系列】的相关教程结束。

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

下载本文的Word格式文档,以方便收藏与打印。