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的更多相关文章

  1. Entity Framework Code-First(9.6):DataAnnotations - StringLength Attribute

    DataAnnotations - StringLength Attribute: StringLength attribute can be applied to a string type pro ...

  2. Entity Framework Code-First(9.5):DataAnnotations - MaxLength Attribute

    DataAnnotations - MaxLength Attribute: MaxLength attribute can be applied to a string or array type ...

  3. Entity Framework Code-First(9.10):DataAnnotations - NotMapped Attribute

    DataAnnotations - NotMapped Attribute: NotMapped attribute can be applied to properties of a class. ...

  4. Entity Framework Code-First(9.9):DataAnnotations - ForeignKey Attribute

    DataAnnotations - ForeignKey Attribute: ForeignKey attribute can be applied to properties of a class ...

  5. Entity Framework Code-First(9.8):DataAnnotations - Column Attribute

    DataAnnotations - Column Attribute: Column attribute can be applied to properties of a class. Defaul ...

  6. Entity Framework Code-First(9.7):DataAnnotations - Table Attribute

    DataAnnotations - Table Attribute: Table attribute can be applied to a class. Default Code-First con ...

  7. Entity Framework Code-First(9.2):DataAnnotations - TimeStamp Attribute

    DataAnnotations - TimeStamp Attribute: TimeStamp attribute can be applied to only one byte array pro ...

  8. 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 ...

  9. Entity Framework Code-First(9.3):DataAnnotations - ConcurrencyCheck Attribute

    ConcurrencyCheck Attribute: ConcurrencyCheck attribute can be applied to a property of a domain clas ...

随机推荐

  1. 0417 封装 property、classmethod、staricmethod

    一.封装 把一堆东西装在一个容器里 函数和属性装到了一个非全局的命名空间 class A: __N = 123 # 静态变量 def func(self): print(A.__N) # 在类的内部使 ...

  2. nginx 搭建虚拟主机

    一.排错三部曲 第一步在客户端上ping服务端ip  ping 10.0.0.8 第二部在客户端上telnet服务器端IP.端口  telnet 10.0.0.8 第三部在客户端使用wget命令检测 ...

  3. 同步异步阻塞非阻塞Reactor模式和Proactor模式 (目前JAVA的NIO就属于同步非阻塞IO)

    在高性能的I/O设计中,有两个比较著名的模式Reactor和Proactor模式,其中Reactor模式用于同步I/O,而Proactor运用于异步I/O操作. 在比较这两个模式之前,我们首先的搞明白 ...

  4. POJ 3070 矩阵mob

    . 矩阵高速幂想法与快速幂相同 #include<iostream> #include<cstdio> #include<cstring> #define MOD ...

  5. Luogu-4166 [SCOI2007]最大土地面积

    求平面内四边形的最大面积 显然四个端点都应该在凸包上,就先求凸包,然后\(n^2\)枚举四边形对角线,对于一个点\(i\),顺序枚举\(j\),同时用旋转卡壳的方法去找离对角线最远的两个点.总时间复杂 ...

  6. java的远程访问接口的实例

    被别人远程调用和自己远程调用别人接口的理解: 被别人调用接口:其实没有什么神秘的,就是我们写一个正常的方法提供一个访问路径. 调用别人的接口:本质时一个Request请求,实际要使用到javax.ne ...

  7. Xcode 中代码提示不显示

    解决办法: Xcode->Window->Organizer->Projects选中你的项目,点击如下图Derived Data右侧的Delete按钮 DerivedData从字面上 ...

  8. Luogu P1196 [NOI2002]银河英雄传说:带权并查集

    题目链接:https://www.luogu.org/problemnew/show/P1196 题意: 有30000个战舰队列,编号1...30000. 有30000艘战舰,编号1...30000, ...

  9. Python—numpy.bincount()

    1.它大致说bin的数量比x中的最大值大1,每个bin给出了它的索引值在x中出现的次数.下面,我举个例子让大家更好的理解一下: # 我们可以看到x中最大的数为7,因此bin的数量为8,那么它的索引值为 ...

  10. ES查看segment大小

    摘自:http://www.aboutyun.com/thread-17078-1-1.html Segment Memory Segment不是file吗?segment memory又是什么?前面 ...