EF Core中通过Fluent API完成对表的配置
EF Core中通过Fluent API完成对表的配置
设置实体在数据库中的表名
通过ToTable可以为数据模型在数据库中自定义表名,如果不配置,则表名为模型名的复数形式
public class EmployeeConfig:IEntityTypeConfiguration<Employee>
{
public void Configure(EntityTypeBuilder<Employee> builder)
{
// 默认情况下,Employee实体在数据库中会生成Employees的数据表,这里通过ToTable(),将其指定为Employee
builder.ToTable("Employee");
}
}
表间关系映射
在EF Core中,通过Fluent API做表间关系映射时,可以将API分为两类两种
两类:has和with
三种:One、Many
通过两类两种的组合,就可以完成绝大多数表间关系的映射,下面放一些常用的关系配置
public class EmployeeConfig:IEntityTypeConfiguration<Employee>
{
public void Configure(EntityTypeBuilder<Employee> builder)
{
builder.ToTable(nameof(Employee));
// 配置Employee表有一个Department对象,Department有多个Employee对象
// 这是典型的一对多配置
builder.HasOne(e => e.Department).WithMany(d=>d.Employees);
}
}
通常配置一对多的时候只需要在一张表上进行配置就可以了,但也可以在两张表上都进行配置,这样更清晰
public class DepartmentConfig:IEntityTypeConfiguration<Department>
{
public void Configure(EntityTypeBuilder<Department> builder)
{
builder.ToTable(nameof(Department));
// 配置Department表有多个Employee对象,Employee有一个Department对象
builder.HasMany(d => d.Employees).WithOne(e=>e.Department);
// 配置Department表有一个领导(也是Employee对象),领导也属于一个部门
builder.HasOne(d => d.Leader).WithOne(e => e.Department);
}
}
在表关系的配置中,遵循 Has…( ).With…( )的配置方式,Has指的是配置的这张表,然后通过lambda表达式来指定对应的属性,如上面的Department表,通过lambda表达式 d => d.Employees 指定要配置的字段是Employess,HasMany则是指Department表的Employees对应多个
With则是前面has指定的属性对应的表,WithOne反过来指定了Employee表中的Department字段是一个
所以这里就是一个多对一的配置
通过这四个单词的组合,就可以完成一对多、一对一、多对多(通过中间表拆分成两个一对多)的表间关系配置
这里将Entity文件放出来,以便更好理解
public class Employee
{
public Guid Id { get; set; }
public string Name { get; set; }
public bool Gender { get; set; }
public string Phone { get; set; }
public decimal Rating { get; set; }
public decimal Salary { get; set; }
public Guid DepartmentId { get; set; }
public Department Department { get; set; }
}
public class Department
{
public Guid Id { get; set; }
public string Name { get; set; }
public Guid LeaderId { get; set; }
public Employee Leader { get; set; }
public List<Employee> Employees { get; set; }
public string NoUse { get; set; }
}
主键设置
EF Core会自动将实体中名为Id的属性设置为主键,但是有时候主键并不是这么规整的命名,或者需要联合主键的情况,就需要手动指定主键
public class DepartmentConfig:IEntityTypeConfiguration<Department>
{
public void Configure(EntityTypeBuilder<Department> builder)
{
// 配置数据库中主键为实体的Id字段
builder.HasKey(d => d.Id);
}
}
这里指定了Department的Id字段为主键,实体属性名为Id不用手动指定,这里只是展示一下自定义的语法
public class DepartmentConfig:IEntityTypeConfiguration<Department>
{
public void Configure(EntityTypeBuilder<Department> builder)
{
// 配置数据库中主键为实体的Id和Name的联合主键
builder.HasKey(d => new {d.Id, d.Name});
}
}
联合主键的设置也很简单,通过new一个匿名对象的方式提供
主键的值
EF Core会默认为主键生成值,但有时候我们希望使用主键并且自己自定义相关的值,比如说自选账号、课程Id等,这时可以这样配置
public class DepartmentConfig:IEntityTypeConfiguration<Department>
{
public void Configure(EntityTypeBuilder<Department> builder)
{
builder.Property(d => d.Id)
.ValueGeneratedNever(); // 配置数据库中实体的Id字段不自动生成值
}
}
通过ValueGeneratedNever()可以禁用自动生成值,实际上它可以给任何一个属性都配置,但是通常只有主键是默认生成值的
外键设置
如果有外键则必须有另一个与之关联的表,所以外键配置只能在表关系配置后附加
EF Core会默认将关联表+Id的字段设置为外键,同主键一样,有时候不是那么规整,就需要手动指定
public class DepartmentConfig:IEntityTypeConfiguration<Department>
{
public void Configure(EntityTypeBuilder<Department> builder)
{
builder.HasOne(d => d.Leader).WithOne(e => e.Department).HasForeignKey("LeaderId");
}
}
在Department实体中指定了Leader,Leader也是Employee对象,如果依照约定属性为EmployeeId会自定设置为外键字段,但是这里指定了LeaderId,就需要手动设置外键字段为LeaderId
忽略某个字段在数据库中的映射
public class DepartmentConfig:IEntityTypeConfiguration<Department>
{
public void Configure(EntityTypeBuilder<Department> builder)
{
// 数据库中忽略该字段,该字段不会存在该实体属性对应的字段
builder.Ignore(d => d.NoUse);
}
}
前面Department实体中有一个NoUse字段,但是不希望它在数据库中映射该字段,就可以通过Ignore的方式忽略掉
字段约束
通过Fluent API能够对字段进行约束,这样在生成数据库表时就会将相应的约束生成,如设置了字段的最大长度在数据库表中的字段数据类型时nvarchar(设定的最大长度),如果没有设置,在数据库表中的字段数据类型则是nvarchar(max)
Fluent支持流式语法,可以将多个约定流式附加
非空约束
public class DepartmentConfig:IEntityTypeConfiguration<Department>
{
public void Configure(EntityTypeBuilder<Department> builder)
{
// 设置姓名字段不为空
builder.Property(d => d.Name).IsRequired();
}
}
字段最大长度
public class DepartmentConfig:IEntityTypeConfiguration<Department>
{
public void Configure(EntityTypeBuilder<Department> builder)
{
// 设置姓名字段最大长度为30,且不为空
builder.Property(d => d.Name).HasMaxLength(30).IsRequired();
}
}
固定长度字段
public class EmployeeConfig:IEntityTypeConfiguration<Employee>
{
public void Configure(EntityTypeBuilder<Employee> builder)
{
// Phone字段在数据库中为11位固定长度字符串 IsFixedLength()用于指定该字段是否为固定长度
builder.Property(e => e.Phone).HasMaxLength(11).IsFixedLength();
}
}
IsFixedLength()用于指定该字段是否为固定长度,其长度为前面设置的字段最大长度
指定字段名
public class EmployeeConfig:IEntityTypeConfiguration<Employee>
{
public void Configure(EntityTypeBuilder<Employee> builder)
{
// 显式指定实体属性对应数据库中的字段名,这里指定Phone字段对应的数据库字段为ChinaPhone
builder.Property(e => e.Phone).HasColumnName("ChinaPhone");
}
}
指定数据类型
public class EmployeeConfig:IEntityTypeConfiguration<Employee>
{
public void Configure(EntityTypeBuilder<Employee> builder)
{
// 指定decimal的精度为5刻度为2
builder.Property(e => e.Rating).HasColumnType("decimal(5, 2)");
}
}
注:关于精度和刻度的解释,精度是数字的位数,刻度是小数的位数,即decimal(5, 2)能表示的最大数是999.99,一共五位,小数两位
指定数据类型更常用的情况是将实体的decimal类型指定为数据库中的money类型
public class EmployeeConfig:IEntityTypeConfiguration<Employee>
{
public void Configure(EntityTypeBuilder<Employee> builder)
{
// 指定decimal的精度为5刻度为2
builder.Property(e => e.Salary).HasColumnType("money");
}
}
为什么我的Fluent API配置长这样
因为进行了分组配置,将每个类的配置分别拆分到不同的文件
具体的配置可以看看微软的官方文档
EF Core中通过Fluent API完成对表的配置的更多相关文章
- 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 ...
- 9.翻译系列:EF 6以及EF Core中的数据注解特性(EF 6 Code-First系列)
原文地址:http://www.entityframeworktutorial.net/code-first/dataannotation-in-code-first.aspx EF 6 Code-F ...
- 9.4 翻译系列:EF 6以及 EF Core中的NotMapped特性(EF 6 Code-First系列)
原文链接:http://www.entityframeworktutorial.net/code-first/notmapped-dataannotations-attribute-in-code-f ...
- EF Core中的DB First与Code First
前言: 大家都习惯在程序中生成对应的model来对数据库进行操作,所以如何快速的生成数据库表的对应model,是基础之一.总结了一下在我的认知中大概是这个结构: Db first方式: 先创建好对应的 ...
- 1.【使用EF Code-First方式和Fluent API来探讨EF中的关系】
原文链接:http://www.c-sharpcorner.com/UploadFile/3d39b4/relationship-in-entity-framework-using-code-firs ...
- EF Core中避免贫血模型的三种行之有效的方法(翻译)
Paul Hiles: 3 ways to avoid an anemic domain model in EF Core 1.引言 在使用ORM中(比如Entity Framework)贫血领域模型 ...
- EF Core 中多次从数据库查询实体数据,DbContext跟踪实体的情况
使用EF Core时,如果多次从数据库中查询一个表的同一行数据,DbContext中跟踪(track)的实体到底有几个呢?我们下面就分情况讨论下. 数据库 首先我们的数据库中有一个Person表,其建 ...
- EF Core中如何正确地设置两张表之间的关联关系
数据库 假设现在我们在SQL Server数据库中有下面两张表: Person表,代表的是一个人: CREATE TABLE [dbo].[Person]( ,) NOT NULL, ) NULL, ...
- Entity Framework 实体框架的形成之旅--Code First模式中使用 Fluent API 配置(6)
在前面的随笔<Entity Framework 实体框架的形成之旅--Code First的框架设计(5)>里介绍了基于Code First模式的实体框架的经验,这种方式自动处理出来的模式 ...
随机推荐
- Python——处理CSV、PDF文件
一.CSV文件处理 (1)读取 import csv filename = "E:/GitHub/Python-Learning/LIVE_PYTHON/2018-06-05/学位英语成绩合 ...
- std::invoke_result的实现详解
目录 目录 前言 invoke_result 标准库中的invoke_result 我的实现 后记 前言 本篇博文将详细介绍一下libstdc++中std::invoke_result的实现过程,由于 ...
- Go语言中时间轮的实现
最近在工作中有一个需求,简单来说就是在短时间内会创建上百万个定时任务,创建的时候会将对应的金额相加,防止超售,需要过半个小时再去核对数据,如果数据对不上就需要将加上的金额再减回去. 这个需求如果用Go ...
- oslab oranges 一个操作系统的实现 实验二 认识保护模式
https://github.com/yyu/osfs00 实验目的: 理解x86架构下的段式内存管理 掌握实模式和保护模式下段式寻址的组织方式. 关键数据结构.代码组织方式 掌握实模式与保护模式的切 ...
- Java开发工程师最新面试题库系列——集合部分(附答案)
集合 如果你有更好的想法请在评论区留下您的答案,一起交流讨论 说说常见的集合有哪些? 答:主要分List.Set.Map.Queue四类,其中包含ArrayList.LinkedList.HashSe ...
- 最新 Apple iPhone 12 价格 All In One
最新 Apple iPhone 12 价格 All In One 美版价格 Apple iPhone 12 mini $699 Apple iPhone 12 $799 Apple iPhone 12 ...
- 如何用 js 实现一个 new 函数
如何用 js 实现一个 new 函数 原理 new 关键字实现经过了如下过程 创建一个空对象 obj = {} 链接到原型 obj.proto = constructor.prototype 绑定 t ...
- nvm install node error
nvm install node error ➜ mini-program-all git:(master) nvm install 10.15.3 Downloading and installin ...
- js replace all & replaceAll
js replace all & replaceAll https://scotch.io/tutorials/javascript-replace-all-instances-of-a-st ...
- js 拖拽排序
See alsoe: https://www.runoob.com/html/html5-draganddrop.html https://developer.mozilla.org/zh-CN/do ...