Entity Framework Code-First(9.1):DataAnnotations - Key Attribute
DataAnnotations - Key Attribute:
Key attribute can be applied to properties of a class. Default Code-First convention creates a primary key column for a property whose name is "Id" or {Class Name} + "Id". Key attribute overrides this default convention. You can apply Key attribute to a property with any name, which you want to create a primary key for.
Consider the following example.
using System.ComponentModel.DataAnnotations; public class Student
{
public Student()
{ } [Key]
public int StudentKey { get; set; } public string StudentName { get; set; } }
As you can see in the above example, Key attribute is applied to StudentKey property of the Student class. So, Code First will override default conventions and create a primary key column StudentKey in the Student table as shown below.
You can also create a composite primary key and make two columns as PK using Key attribute and Column attribute as shown below.
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; } }
The above code creates composite primary key columns StudentKey1 and StudentKey2 in Student table as shown below.
Note: Key attribute creates a PK with identity column when applied to a single integer type property. Composite key does not create an identity column for integer property. Also, Key attribute can be applied to a property of any data type except unsinged integers, e.g. string, datetime, decimal etc.
Entity Framework Code-First(9.1):DataAnnotations - Key Attribute的更多相关文章
- Entity Framework Code-First(9.6):DataAnnotations - StringLength Attribute
DataAnnotations - StringLength Attribute: StringLength attribute can be applied to a string type pro ...
- Entity Framework Code-First(9.5):DataAnnotations - MaxLength Attribute
DataAnnotations - MaxLength Attribute: MaxLength attribute can be applied to a string or array type ...
- Entity Framework Code-First(9.10):DataAnnotations - NotMapped Attribute
DataAnnotations - NotMapped Attribute: NotMapped attribute can be applied to properties of a class. ...
- Entity Framework Code-First(9.9):DataAnnotations - ForeignKey Attribute
DataAnnotations - ForeignKey Attribute: ForeignKey attribute can be applied to properties of a class ...
- Entity Framework Code-First(9.8):DataAnnotations - Column Attribute
DataAnnotations - Column Attribute: Column attribute can be applied to properties of a class. Defaul ...
- Entity Framework Code-First(9.7):DataAnnotations - Table Attribute
DataAnnotations - Table Attribute: Table attribute can be applied to a class. Default Code-First con ...
- Entity Framework Code-First(9.2):DataAnnotations - TimeStamp Attribute
DataAnnotations - TimeStamp Attribute: TimeStamp attribute can be applied to only one byte array pro ...
- Entity Framework Code-First(9.4):DataAnnotations - Required Attribute
Required attribute can be applied to a property of a domain class. EF Code-First will create a NOT N ...
- Entity Framework Code-First(9.3):DataAnnotations - ConcurrencyCheck Attribute
ConcurrencyCheck Attribute: ConcurrencyCheck attribute can be applied to a property of a domain clas ...
随机推荐
- sublime text的pylinter插件设置pylint_rc后提示错误
sublime text插件pylinter提示错误 Warning: option include-ids is deprecated and ignored. 错误本身是Python的错误,这说明 ...
- maven命令创建项目
1)创建一个Project mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArti ...
- scala基本学习
def addOne(f: Int => Int, arg: Int) = f(arg) + 1,意思是 addOne要两个参数一个是:传一个整数的参数且返回一个整形的方法的参数,第二个参数就是 ...
- Spring Boot- 用idea新建spring boot web项目
1.新建project 2.选择Spring Initializr,next 3.输入项目信息,next 4.选择web依赖以及Spring Boot的版本,next 5.Finish 6.Enabl ...
- css中单位px和em,rem的区别
PX:PX实际上就是像素,用PX设置字体大小时,比较稳定和精确.但是这种方法存在一个问题,当用户在浏览器中浏览我们制作的Web页面时,如果改变了浏览器的缩放,这时会使用我们的Web页面布局被打破.这样 ...
- java--xml文件读取(DOM)
1.表现:一“.xml”为扩展名的文件 2.存储:树形结构 3.xml解析应用: 不同应用程序之间的通信-->订票软件和支付软件 不同的平台间通信-->操作系统 不同平台间数据的共享--& ...
- FileInputStream 把文件作为字节流进行读操作
//把文件作为字节流进行读操作 FileInputStream in = new FileInputStream(filename);//FileInputStream具体实现了在文件上读取数据
- QListWidget拖放
setDragEnabled() 允许拖 setAcceptDrops()允许放 setDragDropMode(QAbstractItemView.DragDrop)设置拖拽模式 setSelect ...
- python 标准库 —— 线程与同步(threading、multiprocessing)
1. 创建线程 使用 os 下的 fork() 函数调用(仅限 Unix 系统) import os print('current process (%s) starts ...' % (os.get ...
- 基于libRTMP的流媒体直播之 AAC、H264 推送
这段时间在捣腾基于 RTMP 协议的流媒体直播框架,其间参考了众多博主的文章,剩下一些细节问题自行琢磨也算摸索出个门道,现将自己认为比较恼人的 AAC 音频帧的推送和解析.H264 码流的推送和解析以 ...