数据注解属性--Key【Code-First系列】
2015-12-06 15:24
190 查看
Key特性可以被用到类的属性中,Code-First默认约定,创建一个主键,是以属性的名字“Id”,或者是类名+Id来的。
Key特性重写了这个默认的约定,你可以应用Key特性到一个类的属性上面,不管这个属性的名字是什么,你都可以创建一个主键。
让我们看看下面的代码吧:
在上面的例子中,Key属性应用到了Student实体的StudnetKey属性上面,那么下面将会得到这个主键:
当然你还可以创建复合主键,使用Key特性和Column特性,使2个列同时作为主键,看看下面的代码吧:
注意:我这里先让大家看看一个错误的例子。创建复合主键的时候,我只用Key特性,看可以不可以呢???
看我的代码:
当然我们需要,改一下Main函数的调用代码:
运行程序:
报错了,看一下具体的错误消息:
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方法,来指定主键的序列。
然后我们再修改一下代码:
看现在我们加了Column特性了吧,现在我们再运行程序:
还是抱上面的错误,然后我们再修改代码:
这样还是不行,哈哈,因为没有指定列的顺序,算了,不折腾了,上正确的代码:
运行程序之后,看下数据库:
请注意:单个主键,EF Code-First为我们创建的:主键是自动增长的,而复合主键,得到的主键不是自动增长的。
当然,key 属性不仅仅可以引用到int类型的属性上,还可以应用到字符串,日期类型等等。。
好了,这就是数据注解特性的Key特性了。
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特性了。
相关文章推荐
- JAVA设计模式之单例模式
- 直接选择排序
- OSX 10.11 Developer Beta 1的已知问题列表
- UOJ#152. 【UR #10】汉诺塔
- eclipse 远程debug配置
- Android Studio 设置内存大小及原理,优化运行速度
- javascript 自定义Array对象
- “南大软院大神养成计划“_第二十一天的学习“
- 什么是对偶性?
- fdisk -l详解
- 工作介绍xml书包文件
- recommended-resources-for-mvc
- Java finally语句到底是在return之前还是之后执行?
- 原生JavaScript和jQuery的较量
- 构造函数模式实现拖拽效果
- shell 中的算术运算
- 纪念一下我做出来的第一个贪心算法题 还用了结构体喔。
- shell排序
- 行为
- 将java程序做成可执行文件