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. delphi函数返回多个值的应用

    方法1: function test(var a,b,c:integer):integer; begin end; 方法2: type info = record name:string; age : ...

  2. 算法(Algorithms)第4版 练习 1.3.2

    was best times of the was the it (1 left on stack)

  3. Java强引用、 软引用、 弱引用、虚引用

    Java强引用. 软引用. 弱引用.虚引用 2012-07-09     1.对象的强.软.弱和虚引用    在JDK 1.2以前的版本中,若一个对象不被任何变量引用,那么程序就无法再使用这个对象.也 ...

  4. JVM中垃圾回收算法

    GC 算法与种类 GC的概念 Garbage Collection 垃圾收集1960年 List 使用了GCJava中,GC的对象是堆空间和永久区 引用计数法 老牌垃圾回收算法通过引用计算来回收垃圾使 ...

  5. php设计模式课程---8、适配器模式是什么

    php设计模式课程---8.适配器模式是什么 一.总结 一句话总结: 充电过程中,手机充电器相对于手机和插座之间就是适配器 1.编程中的适配器是怎么回事? 写一个类(适配器),将传入的数据的格式或者内 ...

  6. Confluence 6 找到未使用的空间

    有时候,你希望找到你系统中没有使用的内容.有时候你也希望能够对这些内容进行更多的关注,但是如何找到一些有关长期不更新的页面,或者长期不使用的空间? View Space Activity 页面中的内容 ...

  7. Runtime Error(ACCESS_VIOLATION)

    1.     while(scanf("%d",&n) )// 错误,会出现  Runtime Error(ACCESS_VIOLATION) while(scanf(&q ...

  8. 理解VMware虚拟网络

    简述:VMware虚拟网络概述.实现虚拟网络上网 Part0 子网掩码.DHCP.NAT,这些点请自行百度,百度百科讲的很清晰. Part1 转载:本文出自 "王春海的博客" 博客 ...

  9. 计算机_网络_01_配置IE代理

    一.配置代理 1.打开代理设置 打开chrome浏览器设置->高级设置->系统->打开代理设置 2.打开局域网设置 Internet属性->连接->局域网设置 3.配置代 ...

  10. NserviceBus过期处理

    NserviceBus不过期,修改注册表 HKEY_CURRENT_USER\Software\NServiceBus\3.3下的项TrailDate到2099-08-02