zoukankan      html  css  js  c++  java
  • 【译】第10节---数据注解-Key

    原文:http://www.entityframeworktutorial.net/code-first/key-dataannotations-attribute-in-code-first.aspx

    Key属性可以应用于类的属性。 默认的,Code First约定为名称为“Id”或{Class Name} +“Id”的属性创建一个主键列, Key属性覆盖此默认约定。

    当然,你可以将Key属性应用于要为其创建主键的任何名称的属性。

    请看以下示例:

    using System.ComponentModel.DataAnnotations;
    
    public class Student
    {
        public Student()
        { 
            
        }
    
        [Key]
        public int StudentKey { get; set; }
         
        public string StudentName { get; set; }
            
    }
      

    如上例所示,Key属性应用于Student类的StudentKey属性。 因此,Code First将覆盖默认约定,并在Student表中创建一个主键列StudentKey,如下所示:

    您还可以创建复合主键,并使用Key属性和Column属性将两列作为PK,如下所示:

    using System.ComponentModel.DataAnnotations;
    
    public class Student
    {
        public Student()
        { 
            
        }
        [Key]
        [Column(Order=1)]
        public int StudentKey1 { get; set; }
         
        [Key]
        [Column(Order=2)]
        public int StudentKey2 { get; set; }
         
        public string StudentName { get; set; }
            
    }

    上述代码在Student表中创建复合主键列StudentKey1和StudentKey2,如下所示:

    说明:Key属性在应用于单个整数类型属性时创建具有标识列的PK,复合键不会为整数属性创建一个标识列。

    此外,Key属性可以应用于除非整数之外的任何数据类型的属性,例如。 字符串,日期时间,十进制等。

  • 相关阅读:
    js history对象 手机物理返回键
    正则表达式学习整理
    js获得时间new Date()整理
    币值转换
    打印沙漏
    秋季总结
    对我影响最大的三位老师
    我就是我
    秋季学期学习总结
    自我介绍
  • 原文地址:https://www.cnblogs.com/talentzemin/p/7206066.html
Copyright © 2011-2022 走看看