文章出處
文章列表
外鍵特性,可以應用到類的屬性中。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; } } }
看數據庫:
這個意思就是,我們可以隨便指定外鍵的名稱,哈哈哈哈哈。。。
文章列表
全站熱搜