【译】第10节---数据注解-Key
原文: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的更多相关文章
- 【译】第20节---数据注解-InverseProperty
原文:http://www.entityframeworktutorial.net/code-first/inverseproperty-dataannotations-attribute-in-co ...
- 【译】第18节---数据注解-ForeignKey
原文:http://www.entityframeworktutorial.net/code-first/foreignkey-dataannotations-attribute-in-code-fi ...
- 【译】第19节---数据注解-NotMapped
原文:http://www.entityframeworktutorial.net/code-first/notmapped-dataannotations-attribute-in-code-fir ...
- 【译】第17节---数据注解-Column
原文:http://www.entityframeworktutorial.net/code-first/column-dataannotations-attribute-in-code-first. ...
- 【译】第16节---数据注解-Table
原文:http://www.entityframeworktutorial.net/code-first/table-dataannotations-attribute-in-code-first.a ...
- 【译】第15节---数据注解-StringLength
原文:http://www.entityframeworktutorial.net/code-first/stringlength-dataannotations-attribute-in-code- ...
- 【译】第14节---数据注解-MaxLength/MinLength
原文:http://www.entityframeworktutorial.net/code-first/maxlength-minlength-dataannotations-attribute-i ...
- 【译】第13节---数据注解-Required
原文:http://www.entityframeworktutorial.net/code-first/required-attribute-dataannotations-in-code-firs ...
- 【译】第12节---数据注解-ConcurrencyCheck
原文:http://www.entityframeworktutorial.net/code-first/concurrencycheck-dataannotations-attribute-in-c ...
随机推荐
- Linux基础命令---显示文本look
look 显示文件中以特定字符串开始的行.在look执行二进制搜索时,必须对文件中的行进行排序.如果未指定文件,则使用文件“/usr/share/dict/words“,只比较字母数字字符,忽略字母字 ...
- 转:C# 对委托的BeginInvoke,EndInvoke 及Control 的BeginInvoke,EndInvoke 的理解
转载自:http://www.cnblogs.com/easyfrog/p/3141269.html using System; using System.Collections.Generic; u ...
- 算法训练 P0505
一个整数n的阶乘可以写成n!,它表示从1到n这n个整数的乘积.阶乘的增长速度非常快,例如,13!就已经比较大了,已经无法存放在一个整型变量中:而35!就更大了,它已经无法存放在一个浮点型变量中.因此, ...
- [转载]CSS教程--字体与文本属性
b>font-family功能:用于改变HTML标志或元素的字体,你可设置一个可用字体清单.浏览器由前向后选用字体.语法:{font-family:字体1,字体2, ... ,字体n} font ...
- 76 道 Oracle Goldengate 面试问题
基础 12c新特性 性能 Troubleshoot 其它 1. Oracle Goldengate 支持部署到哪些拓扑? GoldenGate supports the following topol ...
- gradle 定义打包后的项目名
war { archiveName 'ROOT.war' } 或 task makeWar(type:org.gradle.api.tasks.bundling.War) { //指定生成的jar名 ...
- Android NDK MediaCodec在ijkplayer中的实践
https://www.jianshu.com/p/41d3147a5e07 从API 21(Android 5.0)开始Android提供C层的NDK MediaCodec的接口. Java Med ...
- Cent Linux启动tomcat慢的问题
Tomcat7的session id的生成主要通过java.security.SecureRandom生成随机数来实现,随机数算法使用的是”SHA1PRNG”. 是因为一个JDK一个bug,在这个bu ...
- 使用URLConnection进行访问
package test; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOExcepti ...
- AngularJS的简单入门
AngularJS诞生于2009年,由Misko Hevery等人创建,后为Google所收购.是一款优秀的前端JS框架,已经被用于Google的多款产品当中.AngularJS有着诸多特性,最为核心 ...