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 查找视图目录不是什么新鲜和困难的事情,但 _ ...
 
随机推荐
- Jmeter -- 初体验
			
一.Jmeter参数 在命令行输入Jmeter --help得到以下信息: To run Apache JMeter in GUI mode:Double-click on the ApacheJMe ...
 - 扩展ArcGIS API for Silverlight/WPF 中的TextSymbol支持角度标注
			
原文 http://blog.csdn.net/esricd/article/details/7587136 在ArcGIS API for Silverlight/WPF中原版的TextSymbol ...
 - Silverlight 结合ArcGis 使用inforwindow
			
原文 http://www.dotblogs.com.tw/justforgood/archive/2012/05/10/72089.aspx 也许有些人不知道什么事inforwindow,简单来说就 ...
 - 如何检索Android设备的唯一ID
			
关于本文档 Android的开发者在一些特定情况下都需要知道手机中的唯一设备ID.例如,跟踪应用程序的安装,生成用于复制保护的DRM时需要使用设备的唯一ID.在本文档结尾处提供了作为参考的示例代码片段 ...
 - Quartz.NET作业调度框架详解
			
Quartz.NET作业调度框架详解 http://www.cnblogs.com/lmule/archive/2010/08/28/1811042.html
 - C语言调用库函数实现生产者消费者问题
			
#include<stdio.h> #include<stdlib.h> #include<semaphore.h> #include<pthread.h&g ...
 - Java安装根目录
			
bin存放了Java的操作工具,比如编译工具javac.启动JVM的Java等 db存放了Java测试的数据库Derby,企业不用 include存放C++的头文件 jre运行环境,里面有JVM li ...
 - 【转】android电池(四):电池 电量计(MAX17040)驱动分析篇
			
关键词:android 电池 电量计 MAX17040 任务初始化宏 power_supply 平台信息:内核:linux2.6/linux3.0系统:android/android4.0 平台: ...
 - Unity 音乐播放全局类
			
今天晚了LOL, 发现里面的声音系统做得很不错,于是最近就写了一份反正以后也用的到,2D音乐全局播放. 项目跟PoolManager对象池插件结合了. 解决的问题: 1. 已经播放的声音,可以马上暂停 ...
 - jQuery中$.getJSON的返回值问题
			
在使用$.getJSON获得数据库的返回值后,想将该值return传给其他函数.结果遇到问题. $.getJSON(url, data, function(result) { return resul ...