Custom Code-First Conventions:

Code-First has a set of default behaviors for the models that are referred to as conventions. EF 6 provides the ability to define your own custom conventions which will be the default behavior for your models.

There are two main types of Conventions, Configuration Conventions and Model Conventions.

Configuration Conventions:

Configuration Conventions are a way to configure entities without overriding the default behavior provided in the Fluent API. You can define a configuration convention inside your OnModelCreating event or in a custom Convention Class, similar to how you would define normal entity mappings with the Fluent API.

For example, you can define a primary key of an entity that has property named {entity name} _ID, e.g. Student_ID property of Student entity will be primary key:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder
.Properties()
.Where(p => p.Name == p.DeclaringType.Name + "_ID")
.Configure(p => p.IsKey()); base.OnModelCreating(modelBuilder);
}

The following example shows how to set the string length of the Description property in all entities:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder
.Properties()
.Where(p => p.Name == "Description")
.Configure(p => p.HasMaxLength()); base.OnModelCreating(modelBuilder);
}

You can also define custom class for this convention by deriving the Convention class as shown below:

public class PKConvention : Convention
{
public PKConvention()
{
.Properties()
.Where(p => p.Name == p.DeclaringType.Name + "_ID")
.Configure(p => p.IsKey()); }
}

Configure custom convention as shown below:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Add<PKConvention>();
}

Model Conventions:

Model Conventions are based on the underlying model metadata. There are conventions for both CSDL and SSDL. Create a class that implements IConceptualModelConvention from CSDL conventions and implement IStoreModelConvention for SSDL convention.

Visit codeplex documentation for more information.

Download code-first sample project for custom conventions demo.

Entity Framework 6.0 Tutorials(8):Custom Code-First Conventions的更多相关文章

  1. Entity Framework 6.0 Tutorials(1):Introduction

    以下系统文章为EF6.0知识的介绍,本章是第一篇 原文地址:http://www.entityframeworktutorial.net/entityframework6/introduction.a ...

  2. Entity Framework 6.0 Tutorials(4):Database Command Logging

    Database Command Logging: In this section, you will learn how to log commands & queries sent to ...

  3. Entity Framework 6.0 Tutorials(11):Download Sample Project

    Download Sample Project: Download a sample project for Entity Framework 6 Database-First model below ...

  4. Entity Framework 6.0 Tutorials(10):Index Attribute

    Index Attribute: Entity Framework 6 provides Index attribute to create Index on a particular column ...

  5. Entity Framework 6.0 Tutorials(9):Stored Procedure Mapping

    Code First - Insert, Update, Delete Stored Procedure Mapping: Entity Framework 6 Code-First provides ...

  6. Entity Framework 6.0 Tutorials(6):Transaction support

    Transaction support: Entity Framework by default wraps Insert, Update or Delete operation in a trans ...

  7. Entity Framework 6.0 Tutorials(3):Code-based Configuration

    Code-based Configuration: Entity Framework 6 has introduced code based configuration. Now, you can c ...

  8. Entity Framework 6.0 Tutorials(2):Async query and Save

    Async query and Save: You can take advantage of asynchronous execution of .Net 4.5 with Entity Frame ...

  9. Entity Framework 6.0 Tutorials(7):DbSet.AddRange & DbSet.RemoveRange

    DbSet.AddRange & DbSet.RemoveRange: DbSet in EF 6 has introduced new methods AddRange & Remo ...

随机推荐

  1. lvds配置

    基于Altera FPGA的LVDS配置应用一例 在特权同学发表博文<Cyclone III的LVDS接口注意事项>后,不少网友发邮件询问LVDS具体应用的一些问题.这些网友,归根到底,估 ...

  2. #507. 「LibreOJ NOI Round #1」接竹竿 dp

    题目: 题解: 我们考虑把每对花色相同的牌看作区间. 那么如果我们设 \(f_i\) 表示决策在 \([1,i]\) 内的最优答案. 那么有 \(f_i = max\{max\{(f_{j-1}+\s ...

  3. Object.prototype.hasOwnProperty()

    hasOwnProperty() 方法会返回一个布尔值,指示对象是否具有指定的属性作为自身(不继承)属性. 语法 obj.hasOwnProperty(prop) 参数 prop 要检测的属性  字符 ...

  4. 对实体 "useSSL" 的引用必须以 ';' 分隔符结尾。

    <property name="connection.url">jdbc:mysql://127.0.0.1/cache?useUnicode=true&cha ...

  5. jq添加和移除事件的方法,prop和attr

    会在写条件判断的时候遇到,今天在判断没有剩余产品的时候,移除事件.当有产品的时候添加事件: 移除onClick事件: $("a").removeAttr("onclick ...

  6. selenium - xpath - 定位

    前言: XPath 是一门在 XML 文档中查找信息的语言.XPath 可用来在 XML 文档中对元素和属性进行遍历. 看这里介绍:w3school 首先来看一下xpath常用的语法: 一.xpath ...

  7. mysql命令 show slave status\G;命令输出详解

    show slave status\G; 命令输出详解 mysql> show slave status\G; *************************** . row ******* ...

  8. CString 中的SpanIncluding 和SpanExcluding 用法

    SpanIncluding 简单的理解就是提取包含在指定串中的一个子串 MSDN上的备注说:从左边的第一个字符开始查找与给定串相等的字符,如果没有则返回空的串,反之,继续查找,到结束. 例子方便理解 ...

  9. (转)WebApi 上传文件

    本文转载自:http://www.cnblogs.com/zj1111184556/p/3494502.html public class FileUploadController : ApiCont ...

  10. Vue.js:样式绑定

    ylbtech-Vue.js:样式绑定 1.返回顶部 1. Vue.js 样式绑定 Vue.js class class 与 style 是 HTML 元素的属性,用于设置元素的样式,我们可以用 v- ...