19.翻译系列:EF 6中定义自定义的约定【EF 6 Code-First约定】
原文链接:https://www.entityframeworktutorial.net/entityframework6/custom-conventions-codefirst.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系列】
在前面的章节中,你以及学习了Code-First默认的约定。EF 6同样也让你自己定义自定义的约定,然后你的实体就会遵循这个自定义的约定的行为。
这里有两种类型的约定:配置约定(Configuration Conventions)和模型约定(Model Conventions).
配置约定
配置约定就是不重写Fluent API提供实体的默认的行为,给实体进行配置。你可以在OnModelCreating方法中定义配置约定,还可以像Fluent API配置普通的实体映射那样,在自定义的类中配置约定。
例如,如果你想要给属性名称为{实体名称}_ID的属性,配置主键,可以像下面这样:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder
.Properties()
.Where(p => p.Name == p.DeclaringType.Name + "_ID")
.Configure(p => p.IsKey()); base.OnModelCreating(modelBuilder);
}
同样你可以定义数据类型的大小的约定【data type of size】
下面的代码,为string类型的属性定义了一个约定。它将会创建nvarchar类型的列,大小是50。
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder
.Properties()
.Where(p => p.PropertyType.Name == "String")
.Configure(p => p.HasMaxLength()); base.OnModelCreating(modelBuilder);
}
当然,你可以在单独的类中,定义这些约定,这个自定义的类需要继承自Convention类,例如:
public class PKConvention : Convention
{
public PKConvention()
{
.Properties()
.Where(p => p.Name == p.DeclaringType.Name + "_ID")
.Configure(p => p.IsKey());
}
}
添加完自定义的类,然后在OnModelCreating方法中这样用:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Add<PKConvention>();
}
模型约定
模型约定是基于模型元数据的。这里有关于CSDL和SSDL的约定,创建一个类,实现CSDL约定中的IConceptualModelConvention 接口,或者实现SSDL约定中的IStoreModelConvention
接口。
想要了解更多EF 6 自定义约定相关的,可以看看这篇文章: Custom Convention in EF 6 。
19.翻译系列:EF 6中定义自定义的约定【EF 6 Code-First约定】的更多相关文章
- 20.翻译系列:Code-First中的数据库迁移技术【EF 6 Code-First系列】
原文链接:https://www.entityframeworktutorial.net/code-first/migration-in-code-first.aspx EF 6 Code-First ...
- 15.翻译系列:EF 6中的级联删除【EF 6 Code-First 系列】
原文链接:https://www.entityframeworktutorial.net/code-first/cascade-delete-in-code-first.aspx EF 6 Code- ...
- 5.翻译系列:EF 6中数据库的初始化(EF 6 Code-First 系列)
原文地址:http://www.entityframeworktutorial.net/code-first/database-initialization-in-code-first.aspx EF ...
- 7.翻译系列:EF 6中的继承策略(EF 6 Code-First 系列)
原文地址:http://www.entityframeworktutorial.net/code-first/inheritance-strategy-in-code-first.aspx EF 6 ...
- 8.翻译系列: EF 6中配置领域类(EF 6 Code-First 系列)
原文地址:http://www.entityframeworktutorial.net/code-first/configure-classes-in-code-first.aspx EF 6 Cod ...
- 9.4 翻译系列:EF 6以及 EF Core中的NotMapped特性(EF 6 Code-First系列)
原文链接:http://www.entityframeworktutorial.net/code-first/notmapped-dataannotations-attribute-in-code-f ...
- 10.1.翻译系列:EF 6中的实体映射【EF 6 Code-First系列】
原文链接:https://www.entityframeworktutorial.net/code-first/configure-entity-mappings-using-fluent-api.a ...
- 21.翻译系列:Entity Framework 6 Power Tools【EF 6 Code-First系列】
原文链接:https://www.entityframeworktutorial.net/code-first/entity-framework-power-tools.aspx 大家好,这里就是EF ...
- 9.9 翻译系列:数据注解特性之--MaxLength 【EF 6 Code-First系列】
原文链接:https://www.entityframeworktutorial.net/code-first/maxlength-minlength-dataannotations-attribut ...
随机推荐
- JavaScript 六大类运算符(详细~)
运算符 1.算术运算符:+.-.*./.%.++.-- 单目(操作数为一个且为变量): i++.i--.++i.--i; 双目:+.-.*./.%; 双目其中 -*/% ...
- #6164. 「美团 CodeM 初赛 Round A」数列互质-莫队
#6164. 「美团 CodeM 初赛 Round A」数列互质 思路 : 对这个题来言,莫队可以 n*根号n 离线处理出各个数出现个的次数 ,同时可以得到每个次数出现的次数 , 但是还要处理有多少 ...
- 如何删除pagefile.sys
经常使用电脑的用户就会发现系统自带了虚拟内存文件pagefile.sys,若是电脑出现内存不足情况,其就会调用虚拟内存来执行程序,以防止系统内存崩溃.不过,虚拟内存没有真实的内存读取速度快,而且会占用 ...
- C# SQLiteHelper
using System; using System.Data; using System.Data.Common; using System.Data.SQLite; using System.IO ...
- 在notepad++中修改换行符
在notepad++中编辑时,将按tab键设置为插入4个空格而不是tab字符
- Min_25筛 学习笔记
这儿只是一个简单说明/概括/总结. 原理见这: https://www.cnblogs.com/cjyyb/p/9185093.html https://www.cnblogs.com/zhoushu ...
- Kotlin基础(四)Lambda编程
Lambda编程 一.Lambda表达式和成员引用 一)Lambda表达式语法 //注意与Java8中的区别 val sum={ x:Int,y:Int -> x+y } println(sum ...
- python网络编程(十二)
协程 协程,又称微线程,纤程.英文名Coroutine. 协程是啥 首先我们得知道协程是啥?协程其实可以认为是比线程更小的执行单元. 为啥说他是一个执行单元,因为他自带CPU上下文.这样只要在合适的时 ...
- Moo University - Financial Aid [POJ2010] [堆]
题意: 在C头牛里选N头牛,每头牛需要花掉一定经费ai才能得到一定得bi分,在不超过经费F的情况下,使得N头牛的得分中位数最大.(1 <= N <= 19,999,奇数) (N <= ...
- 2017.07.14【NOIP提高组】模拟赛B组
Summary 这次比赛因为迟到了,少了很多时间,也受到了相应的惩罚,这是好的,是个标记牌,警醒着我.这次比赛的题目很难,也就是说,大家的得分都很低,总的来说,收获还是很大的,因为有非常多的技巧被掌握 ...