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. 【java规则引擎】规则引擎RuleBase中利用观察者模式

    (1)当RuleBase中有规则添加或删除,利用观察者模式实现,一旦有变动,规则引擎其他组件也做出相应的改变.(2)学习思想:当一个应用中涉及多个组件,为了实现易扩展,解耦思想.可以利用观察者模式实现 ...

  2. serf  简单使用

    1. 介绍 // 以下为官方介绍,说白了就是进行系统的集群节点管理 Serf uses an efficient gossip protocol to solve three major proble ...

  3. mybatis-generator的坑

    有天发现mybatis-generator不能用,要加什么根项目前缀, 搞到我重新从github下载,然后发现仓库用不了,原来新项目会改我的maven配置(新坑 后来终于明白要在子项目栏使用手脚架,就 ...

  4. BZOJ3790:神奇项链

    浅谈\(Manacher\):https://www.cnblogs.com/AKMer/p/10431603.html 题目传送门:https://lydsy.com/JudgeOnline/pro ...

  5. BufferedInputStream与BufferedOutputStream

    BufferedInputStream是带缓冲区的输入流,默认缓冲区大小是8M,能够减少访问磁盘的次数,提高文件读取性能:BufferedOutputStream是带缓冲区的输出流,能够提高文件的写入 ...

  6. 关于Homebrew出现GitHub API rate limit错误的解决方法

    参考博文: http://havee.me/mac/2013-12/how-to-install-and-use-homebrew.html Error: GitHub API rate limit ...

  7. yum安装报错“rpmts_HdrFromFdno: Header V3 DSA signature: NOKEY, key ID 1e5e0159”

    Do not forget to set gpgkey when installing the oracle-validated rpm Read more: http://oracletoday.b ...

  8. juc线程池原理(一):总体介绍

    概要 线程池类图 线程池的类图如下: 1. Executor 它是"执行者"接口,它是来执行任务的.准确的说,Executor提供了execute()接口来执行已提交的 Runna ...

  9. 1131 Subway Map

    题意:给出起点和终点,计算求出最短路径(最短路径即所经过的站点最少的),若最短路径不唯一,则选择其中换乘次数最少的一条线路. 思路:本题虽然也是求最短路径,但是此路径是不带权值的,路径长度即所经过的边 ...

  10. PHP函数htmlspecialchars_decode

    htmlspecialchars_decode() 函数把一些预定义的 HTML 实体转换为字符. <?php $str = "This is some <b>bold&l ...