标签:
外键特性,可以应用到类的属性中。Code-First默认的约定,对外键属性来说,假定外键属性的名称和主键属性是匹配的。
我们看一下,下面的代码:
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("StudentMaster",Schema="WaHaHa")] public class Student { [Key] [Column(Order=5)] public int StudentKey1 { get; set; } [Key] [Column(Order=6)] public int StudentKey2 { get; set; } [MaxLength(20)] [ConcurrencyCheck] [Required] [Column("SName",Order=1,TypeName="nvarchar")] 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.Linq; using System.Text; namespace EF2 { public class Standard { [Key] public int StdId { get; set; } public string StandardName { get; set; } } }
从上面的图中,可以看出,StdId在Student表中是外键,在Standard表中是主键。
这里是不是用类名+id的做法,使用外键。
默认的是使用这样的:
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("StudentMaster",Schema="WaHaHa")] public class Student { [Key] [Column(Order=5)] public int StudentKey1 { get; set; } [Key] [Column(Order=6)] public int StudentKey2 { get; set; } [MaxLength(20)] [ConcurrencyCheck] [Required] [Column("SName",Order=1,TypeName="nvarchar")] public string StudentName { get; set; } [NotMapped()] public int? Age { get; set; } public int StandardId { get; set; } //类名+ID [ForeignKey("StandardId")] public virtual Standard Standard { get; set; } } }
using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Text; namespace EF2 { public class Standard { [Key] public int StandardId { get; set; } //类名+ID public string StandardName { 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("StudentMaster",Schema="WaHaHa")] public class Student { [Key] [Column(Order=5)] public int StudentKey1 { get; set; } [Key] [Column(Order=6)] public int StudentKey2 { get; set; } [MaxLength(20)] [ConcurrencyCheck] [Required] [Column("SName",Order=1,TypeName="nvarchar")] public string StudentName { get; set; } [NotMapped()] public int? Age { get; set; } public int StandardRefId { get; set; } [ForeignKey("StandardRefId")] public virtual Standard Standard { get; set; } } }
using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Text; namespace EF2 { public class Standard { [Key] public int StandardId { get; set; } public string StandardName { get; set; } } }
看数据库:
这个意思就是,我们可以随便指定外键的名称,哈哈哈哈哈。。。
标签:
原文地址:http://www.cnblogs.com/caofangsheng/p/5023920.html