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. STM32 MCO时钟输出配置实验

    STM32的PA.8引脚具有复用功能——时钟输出(MCO), 该功能能将STM32内部的时钟通过PA.8输出. 操作流程: 1).设置PA.8为复用AF模式. RCC_AHB1PeriphClockC ...

  2. 3D卡片折叠动画自定义下拉框

    在线演示 本地下载

  3. File Space——一个java structs2.0的小栗子(DIY)

    1 概述 File Space系统适用于团体,主要提供的是团队文件分享以及个人文件的存储管理服务.分为个人文件存储管理平台和团队文件共享平台. 个人文件存储平台主要为个人提供相关的文件分类存储服务:该 ...

  4. The Great Pan

                                             The Great Pan Time Limit:1000MS     Memory Limit:65536KB    ...

  5. EntityFramework 学习 一 Querying with EDM 从EDM查询

    前面我们已经创建EDM.DbContext和实体类,接下来我们学习不同的查询实体方法,转变为数据库的SQL查询 Entity Framework支持3种查询方式:1)LINQ to Entities ...

  6. 算法(Algorithms)第4版 练习 1.3.37

    package com.qiusongde.creative; import com.qiusongde.Queue; import edu.princeton.cs.algs4.StdOut; pu ...

  7. Zookeeper简单配置

    1.配置zookeeper 解压tar包后,在conf目录下,有一个zoo_sample.cfg,拷贝一份并命名为zoo.cfg cp zoo_sample.cfg zoo.cfg 编辑修改数据路径, ...

  8. UOJ171 【WC2016】挑战NPC

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...

  9. centos虚拟机启用网卡

    CentOS虚拟机安装成功后,默认开机未启用网关,通过修改配置文件,启用网卡 编辑系统配置文件,虚拟机完成后,系统安装了一个默认的网卡,即eth0,其配置文件的路径为/etc/sysconfig/ne ...

  10. Confluence 6 恢复一个空间的问题解决

    如果你在导入的时候遇到了问题,检查下面的一些提示. 你的文件太大而不能上传?这个是非常常见的错误.出现的原因是备份文件不能在规定的时间内上传到服务器上.为了避免这个错误,放置你的导出文件到  < ...