EF6.0 自定义Code First约定
自定义Code First约定有三种方式,分别是:Lightweight Conventions(轻量级约定)、Configuration Conventions(配置型约定)、Model-based Conventions(基于模型的配置)。
Lightweight Conventions:
这是一种轻量级的约定,可以直接重写DbContext的OnModelCreating方法来实现约定,如下:
public class BaseDbContext : DbContext
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Types().Configure(f => f.ToTable("cms_" + f.ClrType));
//轻松为数据表名设定生成规则
}
}
Lightweight Conventions是最简单的实现方式,大部分的全局配置需求都能够以这种方式来实现。
Configuration Conventions:
自定义Conventions,只需要继承Convention,再重写DbContext的OnModelCreating方法,添加一个Convention即可。
public class StringMaxLengthConvertion : Convention
{
/// <summary>
/// 构造函数
/// </summary>
public StringMaxLengthConvertion()
{
this.Properties().Having(p=>(ColumnLengthAttribute)p.GetCustomAttributes(typeof(ColumnLengthAttribute),true).FirstOrDefault()).Configure((c,a)=>c.HasColumnName(a.ColumnName).HasMaxLength(a.ColumnMaxLength));
}
} public class TestDbContext : DbContext
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Add<StringLengthAttributeConvention>();
}
}
Configuration Conventions实现起来相对繁琐了一点,但是自由度也更高。
IConfigurationConvention接口有两个类型参数:TMemberInfo和TConfiguration。它们用来过滤你想自定义约定的模型元素。
第一个类型参数,TMemberInfo,可以是一下两个值:
Type(System)
PropertyInfo(System.Reflection)
第二个类型参数,TConfiguration,可以是一下任意一种。
ModelConfiguration (System.Data.Entity.ModelConfiguration.Configuration)
EntityTypeConfiguration (System.Data.Entity.ModelConfiguration.Configuration.Types)
PropertyConfiguration (System.Data.Entity.ModelConfiguration.Configuration.Properties)
NavigationPropertyConfiguration (System.Data.Entity.ModelConfiguration.Configuration.Properties.Navigation)
PrimitivePropertyConfiguration (System.Data.Entity.ModelConfiguration.Configuration.Properties.Primitive)
DateTimePropertyConfiguration (System.Data.Entity.ModelConfiguration.Configuration.Properties.Primitive)
DecimalPropertyConfiguration (System.Data.Entity.ModelConfiguration.Configuration.Properties.Primitive)
LengthPropertyConfiguration (System.Data.Entity.ModelConfiguration.Configuration.Properties.Primitive)
BinaryPropertyConfiguration (System.Data.Entity.ModelConfiguration.Configuration.Properties.Primitive)
StringPropertyConfiguration (System.Data.Entity.ModelConfiguration.Configuration.Properties.Primitive)
注意,Type和PropertyConfiguration(以及它的子类)不能混用,否则Configuration Conventions将不会生效。
增加自定义的Data Annotation
利用Custom Code First Conventions,我们还可以扩展自己的Data Annotation。例如,增加一个EmailAttribute特性,然后在Lightweight Conventions或者Configuration Conventions中,判断属性是否应用了EmailAttribute特性;如果是,则将列名映射为“Email”,列类型映射为“nvarchar(255)”, 达到了[Column("Email")]和[MaxLength(255)]共同作用的效果。
更详细的信息,请参考http://msdn.microsoft.com/en-us/data/jj819164.aspx
EF6.0 自定义Code First约定的更多相关文章
- Entity Framework 6新特性:全局性地自定义Code First约定
2012年12月11日,Entity Framework已经发布了Entity Framework 6 Alpha2,因项目需要,目前已使用了其中的两个特性,今天就来介绍一下第一个特性:全局性地自定义 ...
- 【EF】Entity Framework 6新特性:全局性地自定义Code First约定
应用场景 场景一:EF Code First默认使用类名作为表名,如果我们需要给表名加个前缀,例如将类名Category映射到表Shop_Category.将Product映射到Shop_Produc ...
- 实体框架自定义代码优先约定(EF6以后)
仅限EF6仅向前 - 此页面中讨论的功能,API等在实体框架6中引入.如果您使用的是早期版本,则部分或全部信息不适用. 使用Code First时,您的模型是使用一组约定从您的类计算的.默认的Code ...
- 【译】第5节---Code First约定
原文:http://www.entityframeworktutorial.net/code-first/code-first-conventions.aspx 我们在上一节中已经看到了EF Code ...
- EntityFramework Code-First 简易教程(二)-------Code First约定
Code First 约定 在前一篇中,我们已经知道了EF Code-First怎样从模型类(domain classes)中创建数据库表,下面,我们开始学习默认的Code-First约定. 什么是约 ...
- Code First 约定
Code First 约定 借助 Code First,可通过使用 C# 或 Visual Basic .NET 类来描述模型.模型的基本形状可通过约定来检测.约定是规则集,用于在使用 Code Fi ...
- EntityFramWork(3 code First 约定)
Code First 约定 借助 Code First,可通过使用 C# 或 Visual Basic .NET 类来描述模型.模型的基本形状可通过约定来检测.约定是规则集,用于在使用 Code ...
- EF6.0 对于数据库优 模式 新加功能
EF6.0相对于5.0新加了很多功能.先看看两个模式的一些特点. 数据库优先(设计者)和代码优先两者的特点: 连接弹性 异步查询和保存 基于代码的配置 数据库命令记录 数据库命令截取 依赖决议 DbS ...
- NET Core 2.0 自定义
ASP.NET Core 2.0 自定义 _ViewStart 和 _ViewImports 的目录位置 在 ASP.NET Core 里扩展 Razor 查找视图目录不是什么新鲜和困难的事情,但 _ ...
随机推荐
- eclipse 使用mvn模块化开发
1.创建maven父工程步骤:new-->other-->选择maven project-->next-->勾选create a simple project-->nex ...
- WPF 资源收集
转载地址:http://www.cnblogs.com/zhoujg/archive/2009/11/04/1596195.html OpenExpressApp的UI现在是使用WPF,所以熟悉WPF ...
- TEA encryption with 128bit key
If anyone needs some basic encryption in software, here's one solution. This TEA implementation fits ...
- Arcgis api For silverlight 加载QQ地图
原文 http://www.cnblogs.com/thinkaspx/archive/2012/11/07/2759079.html //本篇博客仅在技术上探讨可行性 //如果要使用Q 地图,请 ...
- 2015第10周三jquery ui position
jQuery UI API - .position() 所属类别 方法重载(Method Overrides) | 方法(Methods) | 实用工具(Utilities) 用法 描述:相对另一个元 ...
- 解决mongodb连接失败问题
错误提示: MongoDB shell version: 2.4.9 connecting to: test Mon Mar 3 23:45:09.491 Error: couldn't conne ...
- MongoDB C Driver and APIinstances linux MongoDB安装配置
<一,linux平台MongoDB安装配置>在这我们使用的Centos6 yum部署的,你想搞编译,自个干!
- 【转】通过 ulimit 改善系统性能
概述 系统性能一直是一个受关注的话题,如何通过最简单的设置来实现最有效的性能调优,如何在有限资源的条件下保证程序的运作,ulimit 是我们在处理这些问题时,经常使用的一种简单手段.ulimit 是一 ...
- 不可或缺的企业OA面临问题,以及解决建议 软件定制开发 森普演示平台
---恢复内容开始--- 随着信息时代的来临,企业管理也相应的信息化,各种管理软件相继而出,各行各业的信息化有过成功,也有过失败(注:是以该项目是否达到用户的预期目标而言).据统计在信息化失败的案例中 ...
- poj 3370 Halloween treats(鸽巢原理)
Description Every year there is the same problem at Halloween: Each neighbour is only willing to giv ...