数据诠注属性-Key【Code-First系列】
Key特性可以被用到类的属性中,Code-First默认约定,创建一个主键,是以属性的名字“Id”,或者是类名+Id来的。
Key特性重写了这个默认的约定,你可以应用Key特性到一个类的属性上面,不管这个属性的名字是什么,你都可以创建一个主键。
让我们看看下面的代码吧:
using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; using System.Linq; using System.Text; using System.Threading.Tasks; namespace EF2 { [Table("StudentInfo")] public class Student { [Key] public int StudentKey { get; set; } [Column("Name",TypeName="ntext")] [MaxLength(20)] public string StudentName { get; set; } [NotMapped()] public int? Age { get; set; } public int StdId { get; set; } [ForeignKey("StdId")] public virtual Standard Standard { get; set; } } }
在上面的例子中,Key属性应用到了Student实体的StudnetKey属性上面,那么下面将会得到这个主键:
当然你还可以创建复合主键,使用Key特性和Column特性,使2个列同时作为主键,看看下面的代码吧:
注意:我这里先让大家看看一个错误的例子。创建复合主键的时候,我只用Key特性,看可以不可以呢???
看我的代码:
using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; using System.Linq; using System.Text; using System.Threading.Tasks; namespace EF2 { [Table("StudentInfo")] public class Student { [Key] public int StudentKey1 { get; set; } [Key] public int StudentKey2 { get; set; } [Column("Name",TypeName="ntext")] [MaxLength(20)] public string StudentName { get; set; } [NotMapped()] public int? Age { get; set; } public int StdId { get; set; } [ForeignKey("StdId")] public virtual Standard Standard { get; set; } } }
当然我们需要,改一下Main函数的调用代码:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace EF2 { class Program { static void Main(string[] args) { Student stuModel = new Student() { StudentKey1 = 1,StudentKey2=1, StudentName = "cfs", StdId = 1 }; using (var db = new DbContextClass()) { db.Students.Add(stuModel); db.SaveChanges(); } Console.WriteLine("Add Success"); Console.ReadKey(); } } }
运行程序:
报错了,看一下具体的错误消息:
Unable to determine composite primary key ordering for type 'EF2.Student'. Use the ColumnAttribute (see http://go.microsoft.com/fwlink/?LinkId=386388) or the HasKey method (see http://go.microsoft.com/fwlink/?LinkId=386387) to specify an order for composite primary keys.
大意是:不能创建复合主键,因为没有使用Column特性或者HasKey方法,来指定主键的序列。
然后我们再修改一下代码:
using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; using System.Linq; using System.Text; using System.Threading.Tasks; namespace EF2 { [Table("StudentInfo")] public class Student { [Key] [Column] public int StudentKey1 { get; set; } [Key] [Column] public int StudentKey2 { get; set; } [Column("Name",TypeName="ntext")] [MaxLength(20)] public string StudentName { get; set; } [NotMapped()] public int? Age { get; set; } public int StdId { get; set; } [ForeignKey("StdId")] public virtual Standard Standard { get; set; } } }
看现在我们加了Column特性了吧,现在我们再运行程序:
还是抱上面的错误,然后我们再修改代码:
using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; using System.Linq; using System.Text; using System.Threading.Tasks; namespace EF2 { [Table("StudentInfo")] public class Student { [Key] [Column("one")] public int StudentKey1 { get; set; } [Key] [Column("two")] public int StudentKey2 { get; set; } [Column("Name",TypeName="ntext")] [MaxLength(20)] public string StudentName { get; set; } [NotMapped()] public int? Age { get; set; } public int StdId { get; set; } [ForeignKey("StdId")] public virtual Standard Standard { get; set; } } }
这样还是不行,哈哈,因为没有指定列的顺序,算了,不折腾了,上正确的代码:
using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; using System.Linq; using System.Text; using System.Threading.Tasks; namespace EF2 { [Table("StudentInfo")] public class Student { [Key] [Column(Order=1)] public int StudentKey1 { get; set; } [Key] [Column(Order=2)] public int StudentKey2 { get; set; } [Column("Name",TypeName="ntext")] [MaxLength(20)] public string StudentName { get; set; } [NotMapped()] public int? Age { get; set; } public int StdId { get; set; } [ForeignKey("StdId")] public virtual Standard Standard { get; set; } } }
运行程序之后,看下数据库:
请注意:单个主键,EF Code-First为我们创建的:主键是自动增长的,而复合主键,得到的主键不是自动增长的。
当然,key 属性不仅仅可以引用到int类型的属性上,还可以应用到字符串,日期类型等等。。
好了,这就是数据注解特性的Key特性了。