EF Code-First 学习之旅 Code First Conventions
协定是一系列的默认规则用来自动配置领域中的概念模型
1:类型发现
Code-First对包含DBSet属性的类型创建表(包括这些类型的所有引用类型)
public class Student
{
public Student()
{ }
public int StudentID { get; set; }
public string StudentName { get; set; }
public DateTime DateOfBirth { get; set; }
public byte[] Photo { get; set; }
public decimal Height { get; set; }
public float Weight { get; set; } public Teacher Teacher { get; set; } public Standard Standard { get; set; }
} public class Teacher
{
public Teacher()
{ }
public int TeacherId { get; set; }
public string TeacherName { get; set; }
}
namespace EF_Code_First_Tutorials
{ public class SchoolContext: DbContext
{
public SchoolContext(): base()
{ } public DbSet<Student> Students { get; set; }
public DbSet<Standard> Standards { get; set; } }
}

上图所示:即使上下文不包含Teacher的实体集,Code-First也为Teacher创建表
Code-First发现类型的协定:
1)上下文中的DBSet属性类型
2)引用的类型,即使在不同程序集中
3)DBSet属性的子类
主键约定
默认是:属性名是Id或者是类名+id的属性作为主键
主键的类型可以是任何类型,如果主键的数据类型为数字或者是GUID,则被配置为标识列
如果定义主键为其他名字,则会抛出ModelValidationException 异常
public class Standard
{
public Standard()
{ }
public int StdId { get; set; }
public string StandardName { get; set; } public IList<Student> Students { get; set; } }
Standard定义StdId为主键,EntityFramework会抛出异常
'System.Data.Entity.ModelConfiguration.ModelValidationException' occurred in EntityFramework.dll
EntityType 'Standard' has no key defined. Define the key for this EntityType.
如果要定义StdId为主键,则需要DataAnnotations or Fluent API来配置主键
关系约定
Code First推断两个实体之间的关系用到导航属性
导航属性可以是简单的引用类型或集合
例如在Student中的导航属性为Standard,Standard中的导航属性为ICollection<Student>
因此,Code First自动为Standard和Student建立一对多的关系
在Student表中建立名为Standard_StandardId 的外键
public class Student
{
public Student()
{ }
public int StudentID { get; set; }
public string StudentName { get; set; }
public DateTime DateOfBirth { get; set; }
public byte[] Photo { get; set; }
public decimal Height { get; set; }
public float Weight { get; set; } //Navigation property
public Standard Standard { get; set; }
} public class Standard
{
public Standard()
{ }
public int StandardId { get; set; }
public string StandardName { get; set; } //Collection navigation property
public IList<Student> Students { get; set; } }

因此,Code First默认约定外键的名字为:<导航属性名>_<导航属性的主键>
外键约定
public class Student
{
public Student()
{ }
public int StudentID { get; set; }
public string StudentName { get; set; }
public DateTime DateOfBirth { get; set; }
public byte[] Photo { get; set; }
public decimal Height { get; set; }
public float Weight { get; set; } //Foreign key for Standard
public int StandardId { get; set; } public Standard Standard { get; set; }
} public class Standard
{
public Standard()
{ }
public int StandardId { get; set; }
public string StandardName { get; set; } public IList<Student> Students { get; set; } }
上面的例子中,Student类包含外键StandardId(Standard类的主键),Code First将在Students表中创建StandardId列名代替Standard_StandardId列

上面的外键不可为null,因为int类型不是nullable
如果定义为nullable,则可为null
复杂类型约定
Code-First默认的约定
| Default Convention For | Description |
|---|---|
| Table Name | <实体类名> + 's' EF will create DB table with entity class name suffixed by 's' |
| Primary key Name | 1) Id 2) <实体类名> + "Id" (忽略大小写) EF will create primary key column for the property named Id or <Entity Class Name> + "Id" (case insensitive) |
| Foreign key property Name | By default EF will look for foreign key property with the same name as principal entity primary key name. If foreign key property does not exists then EF will create FK column in Db table with <Dependent Navigation Property Name> + "_" + <Principal Entity Primary Key Property Name> e.g. EF will create Standard_StandardId foreign key column into Students table if Student entity does not contain foreignkey property for Standard where Standard contains StandardId |
| Null column | 所有的引用类型列为null,基类型属性为nullable |
| Not Null Column | 主键属性为notnull,non-nullable |
| DB Columns order | 表中的列排序与实体中的属性排序是一样的,不过主键会被移到首位 |
| Properties mapping to DB | 所有的属性都被映射到列中,使用[NotMapped]特性排除属性或列不进行映射 |
| Cascade delete | Enabled By default for all types of relationships. |
C#中的数据类型与表中的数据类型对应关系
| C# DataType | Related DB Column DataType | PK Column DataType & Length |
|---|---|---|
| int | int | int, Identity column increment by 1 |
| string | nvarchar(Max) | nvarchar(128) |
| decimal | decimal(18,2) | decimal(18,2) |
| float | real | real |
| byte[] | varbinary(Max) | varbinary(128) |
| datetime | datetime | datetime |
| bool | bit | bit |
| byte | tinyint | tinyint |
| short | smallint | smallint |
| long | bigint | bigint |
| double | float | float |
| char | No mapping | No mapping |
| sbyte | No mapping (throws exception) |
No mapping |
| object | No mapping | No mapping |
EF Code-First 学习之旅 Code First Conventions的更多相关文章
- EF Code First学习笔记 初识Code First
Code First是Entity Framework提供的一种新的编程模型.通过Code First我们可以在还没有建立数据库的情况下就开始编码,然后通过代码来生成数据库. 下面通过一个简单的示例来 ...
- EF Code First学习笔记 初识Code First(转)
Code First是Entity Framework提供的一种新的编程模型.通过Code First我们可以在还没有建立数据库的情况下就开始编码,然后通过代码来生成数据库. 下面通过一个简单的示例来 ...
- EF Code First学习笔记
EF Code First学习笔记 初识Code First EF Code First 学习笔记:约定配置 Entity Framework 复杂类型 Entity Framework 数据生成选项 ...
- Entity Framework Code First学习系列目录
Entity Framework Code First学习系列说明:开发环境为Visual Studio 2010 + Entity Framework 5.0+MS SQL Server 2012, ...
- Entity Framework 实体框架的形成之旅--Code First模式中使用 Fluent API 配置(6)
在前面的随笔<Entity Framework 实体框架的形成之旅--Code First的框架设计(5)>里介绍了基于Code First模式的实体框架的经验,这种方式自动处理出来的模式 ...
- Entity Framework Code First学习系列
Entity Framework Code First学习系列目录 Entity Framework Code First学习系列说明:开发环境为Visual Studio 2010 + Entity ...
- Entity Framework 实体框架的形成之旅--Code First的框架设计(5)
在前面几篇介绍了Entity Framework 实体框架的形成过程,整体框架主要是基于Database First的方式构建,也就是利用EDMX文件的映射关系,构建表与表之间的关系,这种模式弹性好, ...
- EF Power Tools的Reverse Engineer Code First逆向生成Model时处理计算字段
VS2013上使用EF Power Tools的Reverse Engineer Code First逆向生成Model时,没有处理计算字段.在保存实体时会出现错误. 可以通过修改Mapping.tt ...
- EF框架step by step(7)—Code First DataAnnotations(2)
上一篇EF框架step by step(7)—Code First DataAnnotations(1)描述了实体内部的采用数据特性描述与表的关系.这一篇将用DataAnnotations描述一下实体 ...
随机推荐
- 转:: 刺鸟:用python来开发webgame服务端(5)
来源:http://ciniao.me/article.php?id=19 --------------- 刺鸟原创文章,转载请注明出处 在前面的文章中,我们已经开始了不少逻辑功能的开发,在这期 ...
- Python 基础函数
函数: 在程序设计中,函数是指用于进行某种计算的一系列语句的有名称的组合. 定义一个函数时,需要指定函数的名称并写下一系列程序语句.定义时不会执行,运行代码时,先加载进内存中,之后使用名称来调用这个函 ...
- C++STL容器(lower_bound,upper_bound)
C++STL容器中有三种二分查找函数,这里分享其中的两个 这两个函数其实都可以理解为不破坏数组次序的前期下能将目标元素插入到数组的第几个位置,不过在细节上两个函数有所差异 int d[6]={0,2, ...
- 段合并 segments merge 被删除的文档的删除时间
2.5 段合并 每个索引分为多个“写一次,读多次”的段 write once and read many times segments 建立索引时,一个段写入磁盘以后就不能更新:被删除的文档的信息存 ...
- event对象及各种事件
事件(event) event对象 (1)什么是event对象? Event 对象代表事件的状态,比如事件在其中发生的元素.键盘按键的状态.鼠标的位置.鼠标按钮的状态.事件通常与函数结合使用,函数不会 ...
- 我的Android进阶之旅------>Android项目运行报java.lang.NoClassDefFoundError错误的解决办法
今天在运行一个Android项目的时候,报了以下错误: D/AndroidRuntime( 3859): Shutting down VM E/AndroidRuntime( 3859): FATAL ...
- 算法训练 s01串
问题描述 s01串初始为"0" 按以下方式变换 0变1,1变01 输入格式 1个整数(0~19) 输出格式 n次变换后s01串 样例输入 3 样例输出 101 数据规模和约定 0~ ...
- Wicket:一种构建和测试动态 Web 页面的简化框架
https://www.ibm.com/developerworks/cn/web/wa-aj-wicket/
- 0107-将Monolith重构为微服务
重构到微服务的概述 将单一应用程序转换为微服务的过程是应用程序现代化的一种形式.这是开发人员几十年来一直在做的事情.因此,在将应用程序重构为微服务时,我们可以重用一些想法. 一个不使用的策略是重写“B ...
- Charles安装与使用
Charles是在 Mac 下常用的网络封包截取工具,在做 移动开发时,我们为了调试与服务器端的网络通讯协议,常常需要截取网络封包来分析. Charles 通过将自己设置成系统的网络访问代理服务器,使 ...