原文:http://www.entityframeworktutorial.net/code-first/key-dataannotations-attribute-in-code-first.aspx

Key属性可以应用于类的属性。 默认的,Code First约定为名称为“Id”或{Class Name} +“Id”的属性创建一个主键列, Key属性覆盖此默认约定。

当然,你可以将Key属性应用于要为其创建主键的任何名称的属性。

请看以下示例:

using System.ComponentModel.DataAnnotations;

public class Student
{
public Student()
{ } [Key]
public int StudentKey { get; set; } public string StudentName { get; set; } }

如上例所示,Key属性应用于Student类的StudentKey属性。 因此,Code First将覆盖默认约定,并在Student表中创建一个主键列StudentKey,如下所示:

您还可以创建复合主键,并使用Key属性和Column属性将两列作为PK,如下所示:

using System.ComponentModel.DataAnnotations;

public class Student
{
public Student()
{ }
[Key]
[Column(Order=)]
public int StudentKey1 { get; set; } [Key]
[Column(Order=)]
public int StudentKey2 { get; set; } public string StudentName { get; set; } }

上述代码在Student表中创建复合主键列StudentKey1和StudentKey2,如下所示:

说明:Key属性在应用于单个整数类型属性时创建具有标识列的PK,复合键不会为整数属性创建一个标识列。

此外,Key属性可以应用于除非整数之外的任何数据类型的属性,例如。 字符串,日期时间,十进制等。

【译】第10节---数据注解-Key的更多相关文章

  1. 【译】第20节---数据注解-InverseProperty

    原文:http://www.entityframeworktutorial.net/code-first/inverseproperty-dataannotations-attribute-in-co ...

  2. 【译】第18节---数据注解-ForeignKey

    原文:http://www.entityframeworktutorial.net/code-first/foreignkey-dataannotations-attribute-in-code-fi ...

  3. 【译】第19节---数据注解-NotMapped

    原文:http://www.entityframeworktutorial.net/code-first/notmapped-dataannotations-attribute-in-code-fir ...

  4. 【译】第17节---数据注解-Column

    原文:http://www.entityframeworktutorial.net/code-first/column-dataannotations-attribute-in-code-first. ...

  5. 【译】第16节---数据注解-Table

    原文:http://www.entityframeworktutorial.net/code-first/table-dataannotations-attribute-in-code-first.a ...

  6. 【译】第15节---数据注解-StringLength

    原文:http://www.entityframeworktutorial.net/code-first/stringlength-dataannotations-attribute-in-code- ...

  7. 【译】第14节---数据注解-MaxLength/MinLength

    原文:http://www.entityframeworktutorial.net/code-first/maxlength-minlength-dataannotations-attribute-i ...

  8. 【译】第13节---数据注解-Required

    原文:http://www.entityframeworktutorial.net/code-first/required-attribute-dataannotations-in-code-firs ...

  9. 【译】第12节---数据注解-ConcurrencyCheck

    原文:http://www.entityframeworktutorial.net/code-first/concurrencycheck-dataannotations-attribute-in-c ...

随机推荐

  1. mysql的sql执行计划详解

    实际项目开发中,由于我们不知道实际查询的时候数据库里发生了什么事情,数据库软件是怎样扫描表.怎样使用索引的,因此,我们能感知到的就只有 sql语句运行的时间,在数据规模不大时,查询是瞬间的,因此,在写 ...

  2. nodejs typescript怎么发送get、post请求,如何获取网易云通信token

    nodejs typescript怎么发送get.post请求,如何获取网易云通信token yarn add jshashesyarn add superagent检查语法yarn lint==== ...

  3. Git从远程仓库里拉取一条本地不存在的分支方法

    Git从远程仓库里拉取一条本地不存在的分支方法 从远程仓库里拉取一条本地不存在的分支时,进入到对应目录先执行git fetch然后再执行git checkout -b 本地分支名 origin/远程分 ...

  4. win10 常用设置 桌面出来计算机图标,固定桌面摆好的图标设置方法,电脑设备ID方法

    win10 常用设置 桌面出来计算机图标,固定桌面摆好的图标设置方法 桌面右键-->显示设置-->桌面图标设置 电脑设备ID:xxx查看方法:桌面右键-->显示设置-->关于

  5. 以太坊智能合约开发,Web3.js API 中文文档 ethereum web3.js入门说明

    以太坊智能合约开发,Web3.js API 中文文档 ethereum web3.js入门说明 为了让你的Ðapp运行上以太坊,一种选择是使用web3.js library提供的web3.对象.底层实 ...

  6. 人工智能深度学习框架MXNet实战:深度神经网络的交通标志识别训练

    人工智能深度学习框架MXNet实战:深度神经网络的交通标志识别训练 MXNet 是一个轻量级.可移植.灵活的分布式深度学习框架,2017 年 1 月 23 日,该项目进入 Apache 基金会,成为 ...

  7. 关于js的日期处理

    1.日期转换(Date)方法一:String变为Date var t = "2015-03-16";var array =  t.split("-");var ...

  8. Let's Chat ZOJ - 3961

    ACM (ACMers' Chatting Messenger) is a famous instant messaging software developed by Marjar Technolo ...

  9. gdb远程debug A syntax error in expression, near `variable)'.

    今天调试有个linux环境的应用时,gdb提示A syntax error in expression, near `variable)'.,最后经查,gdb版本过低(比如7.2)或者源代码不匹配所致 ...

  10. mysql服务器iowait高优化一例完整深入解析

    我们有一服务器,上面运行着两个mysql实例,这几天iowait一直很高,在20-30%,下午特地专门排查和解决了下,相关过程整理如下. 该服务器有两个挂载盘,服务器在阿里云上,一个系统盘,一个数据盘 ...