原文链接:https://www.entityframeworktutorial.net/entityframework6/custom-conventions-codefirst.aspx

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约定】的更多相关文章

  1. 20.翻译系列:Code-First中的数据库迁移技术【EF 6 Code-First系列】

    原文链接:https://www.entityframeworktutorial.net/code-first/migration-in-code-first.aspx EF 6 Code-First ...

  2. 15.翻译系列:EF 6中的级联删除【EF 6 Code-First 系列】

    原文链接:https://www.entityframeworktutorial.net/code-first/cascade-delete-in-code-first.aspx EF 6 Code- ...

  3. 5.翻译系列:EF 6中数据库的初始化(EF 6 Code-First 系列)

    原文地址:http://www.entityframeworktutorial.net/code-first/database-initialization-in-code-first.aspx EF ...

  4. 7.翻译系列:EF 6中的继承策略(EF 6 Code-First 系列)

    原文地址:http://www.entityframeworktutorial.net/code-first/inheritance-strategy-in-code-first.aspx EF 6 ...

  5. 8.翻译系列: EF 6中配置领域类(EF 6 Code-First 系列)

    原文地址:http://www.entityframeworktutorial.net/code-first/configure-classes-in-code-first.aspx EF 6 Cod ...

  6. 9.4 翻译系列:EF 6以及 EF Core中的NotMapped特性(EF 6 Code-First系列)

    原文链接:http://www.entityframeworktutorial.net/code-first/notmapped-dataannotations-attribute-in-code-f ...

  7. 10.1.翻译系列:EF 6中的实体映射【EF 6 Code-First系列】

    原文链接:https://www.entityframeworktutorial.net/code-first/configure-entity-mappings-using-fluent-api.a ...

  8. 21.翻译系列:Entity Framework 6 Power Tools【EF 6 Code-First系列】

    原文链接:https://www.entityframeworktutorial.net/code-first/entity-framework-power-tools.aspx 大家好,这里就是EF ...

  9. 9.9 翻译系列:数据注解特性之--MaxLength 【EF 6 Code-First系列】

    原文链接:https://www.entityframeworktutorial.net/code-first/maxlength-minlength-dataannotations-attribut ...

随机推荐

  1. springboot2 redis

    本项目使用jar包: <parent> <groupId>org.springframework.boot</groupId> <artifactId> ...

  2. asp.net core 依赖注入实现全过程粗略剖析(1)

    转载请注明出处: https://home.cnblogs.com/u/zhiyong-ITNote/ 常用扩展方法 注入依赖服务: new ServiceCollection().AddSingle ...

  3. javascript中6种基本数据类型详解

    javascript中有5中数据类型(也称为基本数据类型):Undefined.Null.Boolean.Number和String,还有一种复杂数据类型——object,object本质是由一组键值 ...

  4. Android强制关闭某个指定应用 “关闭应用”

    强制关闭指定的应用程序: // 传入应用的包名即可kill掉应用 private void forceStopApp(String packageName) { ActivityManager am ...

  5. 使用纯CSS制作展开合并立方体特效

    显示效果 源码 <html> <head> <meta http-equiv="Content-Type" content="text/ht ...

  6. BZOJ2042 : [2009国家集训队]Will的烦恼

    不难发现题中过程对应着动态维护关于$C$的最大生成树. 为了让$D$最大,同时让字典序最大,那么最后得到的一定是按$pair(C,D,编号)$排序的最大生成树. 对于每条非树边$(u,v,C)$,那么 ...

  7. BZOJ1889 : Maximal

    二分答案,判断是否存在合法方案使得每个数都不超过$mid$. 考虑网络流建图: $i$点的流量下限为$\max(a_i-mid,0)$,费用为$1$,故拆点进行限制. $i$向$i+1$.$S$向$i ...

  8. 考前停课集训 Day7 嘞

    Day7 正如一个大佬提醒的那样,棕名是会被嘲讽的 果然…… 在洛谷里…… 算了. 不必在意. 马上就要退役了. NOIP,开始的地方,也是结束的地方. 如果一群OIer比你小 还会嘲讽你, 你就该退 ...

  9. JS_高程6.面向对象的程序设计(1)理解对象

    js的数据属性:P139(1)[[Configurable]](2)[[Enumerable]](3)[[Writable]](4)[[Value]] 使用Object.definerPropert( ...

  10. 导出使用NPOI

    调用: DataTable table = new DataTable(); #region 创建 datatable table.Columns.Add(new DataColumn("账 ...