9.3 翻译系列:数据注解特性之Key【EF 6 Code-First 系列】
原文链接:http://www.entityframeworktutorial.net/code-first/key-dataannotations-attribute-in-code-first.aspx
EF 6 Code-First系列文章目录:
- 1 翻译系列:什么是Code First(EF 6 Code First 系列)
- 2.翻译系列:为EF Code-First设置开发环境(EF 6 Code-First系列)
- 3.翻译系列:EF Code-First 示例(EF 6 Code-First系列)
- 4.翻译系列:EF 6 Code-First默认约定(EF 6 Code-First系列)
- 5.翻译系列:EF 6中数据库的初始化(EF 6 Code-First 系列)
- 6.翻译系列:EF 6 Code-First中数据库初始化策略(EF 6 Code-First系列
- 7.翻译系列:EF 6中的继承策略(EF 6 Code-First 系列)
- 8.翻译系列: EF 6中配置领域类(EF 6 Code-First 系列)
- 9.翻译系列:EF 6以及EF Core中的数据注解特性(EF 6 Code-First系列)
- 9.1 翻译系列:数据注解特性之----Table【EF 6 Code-First 系列】
- 9.2 翻译系列:数据注解特性之---Column【EF 6 Code First系列】
- 9.3 翻译系列:数据注解特性之Key【EF 6 Code-First 系列】
- 9.4 翻译系列:EF 6以及 EF Core中的NotMapped特性(EF 6 Code-First系列)
- 9.5 翻译系列:数据注解之ForeignKey特性【EF 6 Code-First系列】
- 9.6 翻译系列:数据注解之Index特性【EF 6 Code-First系列】
- 9.7 翻译系列:EF数据注解特性之--InverseProperty【EF 6 Code-First系列】
- 9.8 翻译系列:数据注解特性之--Required 【EF 6 Code-First系列】
- 9.9 翻译系列:数据注解特性之--MaxLength 【EF 6 Code-First系列】
- 9.10 翻译系列:EF数据注解特性之StringLength【EF 6 Code-First系列】
- 9.11 翻译系列:数据注解特性之--Timestamp【EF 6 Code-First系列】
- 9.12 翻译系列:数据注解特性之ConcurrencyCheck【EF 6 Code-First系列】
- 10.翻译系列:EF 6中的Fluent API配置【EF 6 Code-First系列】
- 10.1.翻译系列:EF 6中的实体映射【EF 6 Code-First系列】
- 10.2.翻译系列:使用Fluent API进行属性映射【EF 6 Code-First】
- 11.翻译系列:在EF 6中配置一对零或者一对一的关系【EF 6 Code-First系列】
- 12.翻译系列:EF 6 中配置一对多的关系【EF 6 Code-First系列】
- 13.翻译系列:Code-First方式配置多对多关系【EF 6 Code-First系列】
- 14.翻译系列:从已经存在的数据库中生成上下文类和实体类【EF 6 Code-First系列】
- 15.翻译系列:EF 6中的级联删除【EF 6 Code-First 系列】
- 16.翻译系列:EF 6 Code -First中使用存储过程【EF 6 Code-First系列】
- 17.翻译系列:将Fluent API的配置迁移到单独的类中【EF 6 Code-First系列】
- 18.翻译系列:EF 6 Code-First 中的Seed Data(种子数据或原始测试数据)【EF 6 Code-First系列】
- 19.翻译系列:EF 6中定义自定义的约定【EF 6 Code-First约定】
- 20.翻译系列:Code-First中的数据库迁移技术【EF 6 Code-First系列】
- 20.1翻译系列:EF 6中自动数据迁移技术【EF 6 Code-First系列】
- 20.2.翻译系列:EF 6中基于代码的数据库迁移技术【EF 6 Code-First系列】
- 21.翻译系列:Entity Framework 6 Power Tools【EF 6 Code-First系列】
Key特性应用于实体的一个属性上面,使之成为键属性,然后生成数据库的时候,数据表中相应的列就会成为主键。默认的约束为名称为ID或者实体名称+ID的属性,创建主键。Key特性重写了默认的约定。

正如上面的代码所示,我们在Student实体的StudentKey属性上面,应用于Key特性,这样就会重写默认的约定,就会在Students表的StudentKey列上,创建一个主键:

Key特性可以应用于任何原始数据类型的属性上,除了无符号整形。
EF 6中:
在EF 6中 ,Key特性可以应用于实体的多个属性上面,然后就可以在数据库中,创建联合主键。
EF Core不支持使用数据注解的Key特性,来创建联合主键,在EFCore中你必须使用Fluent API中的HasKey方法,来创建联合主键。

上面的代码,将会在Students表中创建联合主键【StudentKey和AdmissionNum】:

注意的是:Key特性,标识一个列生成的单一主键是自增主键,而复合主键不会生成自增列。
学习不动手,那就是耍流氓,无用功,我们自己动手试试:
1.创建一个名称为EFAnnotationKey的控制台应用程序,安装好EF

2.创建一个Student类:
public class Student
{
public int StudentID { get; set; } public string Name { get; set; } public int Age { get; set; } public string Sex { get; set; }
}
3.创建一个上下文类:
public class EFDbContext:DbContext
{
public EFDbContext() : base("name=Constr")
{
}
public DbSet<Student> Students { get; set; }
}
4.App.config配置文件:【数据库连接字符串】
<!--数据库连接字符串-->
<connectionStrings>
<add name="Constr" connectionString="Server=.;Database=EFAnnotationKeyDB;uid=sa;pwd=Password_1" providerName="System.Data.SqlClient"/>
</connectionStrings>
5.测试代码:
class Program
{
static void Main(string[] args)
{
using (var db = new EFDbContext())
{
List<Student> lstStuModel = db.Students.ToList();
} Console.WriteLine("success");
Console.ReadKey();
}
}
6.运行代码:

看看生成的数据库:可以看到
1.默认生成的数据表名称为Students,
2.默认的模式名为dbo,
3.默认将StudentID属性映射为主键,并且主键默认是自增的。
4.字符串类型默认生成nvarchar(max),int类型默认生成not null的列


1.我们使用Key 特性来重写一下默认的约定,修改Student实体:

2.修改上下文类:

3.运行程序:

看看生成的数据库:【可以看到,生成一个StudentNumber主键列,并且是自增的】


现在看看复合主键列:【可以看到我们没有使用Column指定两个属性的顺序,先看看可以生成复合主键列不。】

运行程序:报错了,可以看到,我们没有指定两个属性的先后顺序是不行的。

修改一下Student类:这里我们没有按照索引的顺序指定,运行看下:


成功了,我们看下数据库是啥样的:
可以看到:
1.即使没有按照索引属性指定复合主键的顺序,依然能生成数据库以及表,顺序是,优先按照指定的列的顺序,其他没有指定的属性顺序,是按照他们在实体中的先后顺序,在数据库中排列的。
2.生成的复合主键,都不是自增的。


再来看下这样的情况,只对一个属性列,设置顺序,看看:

运行:

可以看到还是不行,必须同时指定列的顺序。
综上所述,我们可以总结如下【EF 数据注解Key特性】:
1.EF默认生成的主键是ID属性列,或者名称为实体名+ID的的属性列,可以通过Key特性重写。
2.生成单一主键列的时候,是自增的,而生成复合主键,都不是自增的
3.EF 生成复合主键,必须同时设置Key特性以及使用Column特性中的Order参数指定所有标志KEY属性的顺序。
9.3 翻译系列:数据注解特性之Key【EF 6 Code-First 系列】的更多相关文章
- 9.10 翻译系列:EF数据注解特性之StringLength【EF 6 Code-First系列】
原文链接:https://www.entityframeworktutorial.net/code-first/stringlength-dataannotations-attribute-in-co ...
- 9.9 翻译系列:数据注解特性之--MaxLength 【EF 6 Code-First系列】
原文链接:https://www.entityframeworktutorial.net/code-first/maxlength-minlength-dataannotations-attribut ...
- 9.7 翻译系列:EF数据注解特性之--InverseProperty【EF 6 Code-First系列】
原文链接:https://www.entityframeworktutorial.net/code-first/inverseproperty-dataannotations-attribute-in ...
- 9.2 翻译系列:数据注解特性之---Column【EF 6 Code First系列】
原文链接:http://www.entityframeworktutorial.net/code-first/column-dataannotations-attribute-in-code-firs ...
- 9.翻译系列:EF 6以及EF Core中的数据注解特性(EF 6 Code-First系列)
原文地址:http://www.entityframeworktutorial.net/code-first/dataannotation-in-code-first.aspx EF 6 Code-F ...
- 9.1 翻译系列:数据注解特性之----Table【EF 6 Code-First 系列】
原文地址:http://www.entityframeworktutorial.net/code-first/table-dataannotations-attribute-in-code-first ...
- 9.5 翻译系列:数据注解之ForeignKey特性【EF 6 Code-First系列】
原文链接:https://www.entityframeworktutorial.net/code-first/foreignkey-dataannotations-attribute-in-code ...
- 9.6 翻译系列:数据注解之Index特性【EF 6 Code-First系列】
原文链接:https://www.entityframeworktutorial.net/entityframework6/index-attribute-in-code-first.aspx EF ...
- 9.8 翻译系列:数据注解特性之--Required 【EF 6 Code-First系列】
原文链接:https://www.entityframeworktutorial.net/code-first/required-attribute-dataannotations-in-code-f ...
随机推荐
- board_led.h/board_led.c
/******************************************************************************* Filename: board_led ...
- 浅谈MyBatisGenerator的使用
目录 1.概述 2.依赖 3.Maven插件配置 4.配置文件说明 5.运行 6.总结 1.概述 日常中使用MyBatis最为麻烦的就是编写Mapper文件了, 如果数据库增加一张表, 这时通常会复制 ...
- ArcPy开发教程1-面向ArcGIS的Python语言基础
ArcPy开发教程1-面向ArcGIS的Python语言基础 联系方式:谢老师,135-4855-4328,xiexiaokui#qq.com 第一节课 时间2019年2月26日 上午第一节 讲解:A ...
- CSS3 Box-sizing(转载)
转载自:W3CPLUS Airen的博客:http://www.w3cplus.com/content/css3-box-sizing box-sizing是CSS3的box属性之一.一说到CSS的盒 ...
- C语言递归函数讲解
递归函数是什么? 是函数.................... 你可以把它理解成是for循环与死循环的结合的函数.简单的说:递归函数是有条件终止的死循环函数: 死循环函数这里是指在函数体中调用自身: ...
- Netty4.0源码解析 NioServerSocketChannel
一.引言Netty的Channel在JDK NIO的Channel基础上做了一层封装,提供了更多的功能.Netty的中的Channel实现类主要有:NioServerSocketChannel(用于服 ...
- 100-days: eleven
Title: Facebook's live streaming(网络直播) is criticized(批评) after mosque(清真寺) shooting(枪击). live adj.现场 ...
- 735. Asteroid Collision彗星相撞后的消失数组
[抄题]: We are given an array asteroids of integers representing asteroids in a row. For each asteroid ...
- 本机连接Spark Standalone--最简单的spark调试方式
为了既能远程连接spark 查看ui 又能本地练习 安装简单 去官网 http://spark.apache.org/downloads.html 选择对应版本下载 tar包 解压 tar ...
- Spark大数据针对性问题。
1.海量日志数据,提取出某日访问百度次数最多的那个IP. 解决方案:首先是将这一天,并且是访问百度的日志中的IP取出来,逐个写入到一个大文件中.注意到IP是32位的,最多有个2^32个IP.同样可以采 ...