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 ...
随机推荐
- 【leetcode刷题笔记】Unique Paths II
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- 【leetcode刷题笔记】Spiral Matrix
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...
- 全屏滚动插件之 fullpage.js
前言:做移动端网页下滑/点击切换到下一页的效果,采用了fullpage,js最新的版本 https://cdnjs.cloudflare.com/ajax/libs/fullPage.js/3.0.4 ...
- 第十篇、模块一、sys\os\hashlib模块的应用
一.模块分为三种 1)自定义模块 2)第三方模块 3)内置模块 如何导入模块? 下面两种: 1)import 模块名字 as 别名(重新给模块命名) 2)from 模块名字 import 功能( ...
- hihocoder第七周 完全背包模板题
时间限制:20000ms 单点时限:1000ms 内存限制:256MB 描述 且说之前的故事里,小Hi和小Ho费劲心思终于拿到了茫茫多的奖券!而现在,终于到了小Ho领取奖励的时刻了! 等等,这段故事为 ...
- Victor/ArrayList/LinkedList/Stack/CopyOnWriteArrayList 区别
Victor:采用数组的方式存储数据,与ArrayList相同,线程安全.性能比ArrayList差 ArrayList:采用数据的方式存储数据,线程不安全.ArrayList使用数组来存储数据,使用 ...
- java客户端文件的上传和下载
java客户端文件的上传和下载 //上传 public JTable upload(String id){ JTable table=new JTable(); System.out.println( ...
- python3 字符串属性(四)
1. S.partition(sep) -> (head, sep, tail) Search for the separator sep in S, and return the part b ...
- 分享知识-快乐自己:solr 伪集群搭建
前言:在你搭建集群之前先去搭建一个单机版的 跳转 Solr 集群构建图: 1):部署(4个)Tomcat 下载 更改每一个 tomcat 启动端口号: [root@VMSolr tomcat_clus ...
- jQuery-中的事件
[jQuery中的事件] javascript和html之间的交互是通过用户和浏览器操作页面时引发的事件来处理的,虽然传统的javascript能完成这些交互,但事jQuery增加并扩充了基本事件处理 ...