【译】第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 ...
 
随机推荐
- python 读csv数据 通过改变分隔符去掉引号
			
import csv with open(r'C:\Temp\ff.csv') as f: f_csv=csv.reader(f,delimiter='\t') headers=next(f_csv) ...
 - <转>jmeter(九)逻辑控制器
			
本博客转载自:http://www.cnblogs.com/imyalost/category/846346.html 个人感觉不错,对jmeter讲解非常详细,担心以后找不到了,所以转发出来,留着慢 ...
 - 大数据自学6-Hue集成环境操作Hbase
			
上一章讲过,Hue集成环境是可以直接操作Hbase,但是公司的环境一直报错,虽然也可以透过写代码访问Hbase,但是看到Hue环境中无法访问,还是觉得不爽,因此决定再花些力气找找原因. 找原因要先查L ...
 - c#md5加密的简单用法
			
using System.Security.Cryptography; //MD5 md5 = MD5.Create(); MD5 md5 = new MD5CryptoServiceProvider ...
 - ZYNQ跑系统 系列(二) petalinux方式移植linux  petalinux-config遇到问题
			
petalinux-config --get-hw-description=. 报错 [INFO] sourcing bitbakeERROR: Failed to source bitbakeERR ...
 - nginx的gzip压缩功能
			
我们在开发网站的时候,应该要考虑到pv,因为pv比较大可能会造成服务器带宽不够用,进而导致用户体验变差. 这个时候我们就可以考虑用nginx的gzip功能. 在nginx中开启gzip压缩功能很简单, ...
 - Vue小案例 之 商品管理------删除商品与提示
			
实现删除商品功能 根据索引来进行删除商品: 实现删除商品的HTML: <!--显示表格--> <div class="table-warp"> <di ...
 - 01: docker 基本使用
			
1.1 docker基础 1.docker与虚拟机比较 2.docker版本 1. 社区版(Community Edition, CE) 2. 企业版(Enterprise Edition, EE) ...
 - opencv学习之路(2)、读取视频,读取摄像头
			
一.介绍 视频读取本质上就是读取图像,因为视频是由一帧一帧图像组成的.1秒24帧基本就能流畅的读取视频了. ①读取视频有两种方法: A. VideoCapture cap; cap.open(“1.a ...
 - VC++ 使用CreateProcess创建新进程
			
https://www.cnblogs.com/fancing/p/6477918.html #include <windows.h> #include <tchar.h> # ...