原文连接:https://www.entityframeworktutorial.net/entityframework6/custom-conventions-codefirst.aspxhtml
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一样也让你本身定义自定义的约定,而后你的实体就会遵循这个自定义的约定的行为。app
这里有两种类型的约定:配置约定(Configuration Conventions)和模型约定(Model Conventions).ide
配置约定学习
配置约定就是不重写Fluent API提供实体的默认的行为,给实体进行配置。你能够在OnModelCreating方法中定义配置约定,还能够像Fluent API配置普通的实体映射那样,在自定义的类中配置约定。测试
例如,若是你想要给属性名称为{实体名称}_ID的属性,配置主键,能够像下面这样:ui
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】spa
下面的代码,为string类型的属性定义了一个约定。它将会建立nvarchar类型的列,大小是50。.net
protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder .Properties() .Where(p => p.PropertyType.Name == "String") .Configure(p => p.HasMaxLength(50)); 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 。