Entity Framework 6.0 Tutorials(8):Custom Code-First Conventions
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的更多相关文章
- Entity Framework 6.0 Tutorials(1):Introduction
以下系统文章为EF6.0知识的介绍,本章是第一篇 原文地址:http://www.entityframeworktutorial.net/entityframework6/introduction.a ...
- 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 ...
- Entity Framework 6.0 Tutorials(11):Download Sample Project
Download Sample Project: Download a sample project for Entity Framework 6 Database-First model below ...
- Entity Framework 6.0 Tutorials(10):Index Attribute
Index Attribute: Entity Framework 6 provides Index attribute to create Index on a particular column ...
- Entity Framework 6.0 Tutorials(9):Stored Procedure Mapping
Code First - Insert, Update, Delete Stored Procedure Mapping: Entity Framework 6 Code-First provides ...
- Entity Framework 6.0 Tutorials(6):Transaction support
Transaction support: Entity Framework by default wraps Insert, Update or Delete operation in a trans ...
- Entity Framework 6.0 Tutorials(3):Code-based Configuration
Code-based Configuration: Entity Framework 6 has introduced code based configuration. Now, you can c ...
- 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 ...
- Entity Framework 6.0 Tutorials(7):DbSet.AddRange & DbSet.RemoveRange
DbSet.AddRange & DbSet.RemoveRange: DbSet in EF 6 has introduced new methods AddRange & Remo ...
随机推荐
- 【BZOJ2908】又是nand 树链剖分+线段树
[BZOJ2908]又是nand escription 首先知道A nand B=not(A and B) (运算操作限制了数位位数为K)比如2 nand 3,K=3,则2 nand 3=not (2 ...
- UVA10674 Tangents
题意 PDF 分析 就是圆的切线的模板. 注意精度问题,排序的时候也不能直接写,被卡了好几次. 时间复杂度\(O(T)\) 代码 #include<iostream> #include&l ...
- bzoj 2259 [Oibh]新型计算机 ——最短路(建图)
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2259 不是 n^2 条边!连那条边权为0的边之后,只要每个位置向它的前一个位置和后一个位置连 ...
- mysql + keepalived架构
mysql + keepalived架构 文档(这个文章共有三篇): http://blog.itpub.net/27000195/viewspace-1364706/
- 从ROS bag文件中提取图像
从ROS bag文件中提取图像 创建launch文件,如下: export.launch <launch> <node pkg="rosbag" type=&qu ...
- emqtt 4 (我要publish消息了)
这次,分析处理publish msg的流程. 由protocol开始 publish 类型的packet的处理是: process(Packet = ?PUBLISH_PACKET(_Qos, Top ...
- java工具类mht转html格式文件 及简单的HTML解析
package com.szy.project.utils; import java.io.BufferedInputStream; import java.io.BufferedOutputStre ...
- unittest框架+ HTMLTestRunner 出报告时,展示控制台信息 不同展示的参数写法 加verbosity
加verbosity参数 没有加的时候展示: 参考: 来源: https://www.cnblogs.com/tomweng/p/6609937.html 介绍: HTMLTestRunner 是 ...
- 事务之二:spring事务(事务管理方式,事务5隔离级别,7个事务传播行为,spring事务回滚条件)
事物管理对于企业应用来说是至关重要的,好使出现异常情况,它也可以保证数据的一致性. spring支持编程式事务管理和声明式事务管理两种方式. 编程式事务管理使用TransactionTemplate或 ...
- python开发mysql:索引
一,索引管理 索引分类: 普通索引INDEX:加速查找 唯一索引: -主键索引PRIMARY KEY:加速查找+约束(不为空.不能重复) -唯一索引UNIQUE:加速查找+约束(不能重复) 联合索引: ...