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刷题笔记】Reverse Nodes in k-Group
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If ...
- Linux 上关于iptables
有几个命令: 1.service iptables staus 2.service iptables start 3.service iptables restart 有个配置文件/ec ...
- hiho一下 第四十九周 题目1 : 欧拉路·一【无向图 欧拉路问题】
题目1 : 欧拉路·一 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho最近在玩一个解密类的游戏,他们需要控制角色在一片原始丛林里面探险,收集道具,并找到最 ...
- 投影矩阵、最小二乘法和SVD分解
投影矩阵广泛地应用在数学相关学科的各种证明中,但是由于其概念比较抽象,所以比较难理解.这篇文章主要从最小二乘法的推导导出投影矩阵,并且应用SVD分解,写出常用的几种投影矩阵的形式. 问题的提出 已知有 ...
- pyqt5开发之俄罗斯方块
#!/usr/bin/python3 # -*- coding: utf-8 -*- """ ZetCode PyQt5 tutorial This is a Tetri ...
- unit Base64Unit;
unit Base64Unit; unit Base64Unit; //Download by http://www.codefans.net interface uses Classes, SysU ...
- idea中java异常
1. Compilation failed: internal java compiler error 解决方案:File-->Setting...-->Build,Execution,D ...
- 具有增、删、改、查功能的vue-tree树组件
最近写了一个具有增删改查功能的多级树组件,感觉很实用,啦啦啦啦, 废话不多说,看代码: tree.vue <template> <div> <div class=&quo ...
- Flex学习之(JS中调用Flex的方法)
flex端 public function initApp():void { ExternalInterface.addCallback("function1",call ...
- Ffmpeg转码研究一
Ffmpeg是一款功能强大的视频处理工具,那么转码肯定不是问题的,因为项目的需求,对转码进行了研究.刚开始首先去看了ffmpeg源代码中的一个例子transcode.c,但是发现该例子更应该称之为re ...