EF 6 Code-First系列文章目錄:typescript
這裏,你將學習在兩個實體間,配置一對零或者一對多的關係。
咱們使用Student和StudentAddress實體來配置這種關係:數據庫
public class Student
{
public int StudentId { get; set; }
public string StudentName { get; set; }
public virtual StudentAddress Address { get; set; }
}
public class StudentAddress
{
public int StudentAddressId { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public string City { get; set; }
public int Zipcode { get; set; }
public string State { get; set; }
public string Country { get; set; }
public virtual Student Student { get; set; }
}
你能夠看這篇文章,Entity Relationship 來理解EF怎麼管理一對1、一對多、多對多關係的。api
一對零或一對一的關係發生在這樣的兩個實體之間:當一個表的主鍵,是另一個表的主鍵而且是外鍵的時候,因此咱們須要配置Student實體中的StudentID屬性爲主鍵,而後StudentAddressID既是主鍵又是外鍵。app
這裏,咱們將使用數據註解特性來給Student實體和StudentAddress實體,配置一對零或者一對一關係。ide
Student實體遵循默認的約定,它包含一個StudentId屬性,因此到時候這個屬性就會成爲Students表的主鍵,Student實體咱們不用做任何修改,就讓它根據默認配置就行。學習
對於StudentAddress實體,咱們須要配置StudentAddressId爲主鍵和外鍵,由於StudentAddressId遵循約定,因此只用給它配置外鍵。測試
public class Student
{
public int StudentId { get; set; }
public string StudentName { get; set; }
public virtual StudentAddress Address { get; set; }
}
public class StudentAddress
{
[ForeignKey("Student")]
public int StudentAddressId { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public string City { get; set; }
public int Zipcode { get; set; }
public string State { get; set; }
public string Country { get; set; }
public virtual Student Student { get; set; }
}
這樣就使用數據註解配置了一對零或一對一的關係了。ui
請注意:Student包含StudentAddress導航屬性,而且StudentAddress實體包含Student導航屬性。在一對零或一對一關係中,Student能夠在沒有StudentAddress的狀況下被保存,可是StudentAddress沒有Student的狀況下不能保存。EF將會拋出異常,若是你沒有Student實體就保存StudentAddress實體的話。spa
這裏咱們將使用Fluent API配置一對零或者一對一關係。
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// Configure Student & StudentAddress entity
modelBuilder.Entity<Student>()
.HasOptional(s => s.Address) // Mark Address property optional in Student entity
.WithRequired(ad => ad.Student); // mark Student property as required in StudentAddress entity. Cannot save StudentAddress without Student
}
上面的例子中,咱們從Student實體開始配置,HasOptional()方法配置Student實體中的Address導航屬性爲可選的【保存Student實體的時候,能夠沒有StudentAddress實體】,而後WithRequired方法設置StudentAddress實體中的Student導航屬性爲必須的【保存StudentAddress實體的時候,必需要有Student】。上面的代碼,一樣會使StudentAddressId成爲外鍵。
這樣你就配置了一對零或者一對一的關係,這裏Student能夠在沒有StudentAddress的狀況下保存,可是StudentAddress不能在沒有Student的狀況下保存。EF API將會建立下面的數據庫:
咱們可使用Fluent API配置一對一的關係,這樣狀況下,兩個實體對於彼此都是必須的,意味着:Student必須包含StudentAddress,而且StudentAddress必須包含Student。
請注意:SQL Server中,一對一的關係,在技術上是不可能的。上面的代碼設置將會永遠是一對零或一對一關係。EF只是對實體造成一對一的關係,並非在數據庫中是一對一關係。
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// Configure StudentId as FK for StudentAddress
modelBuilder.Entity<Student>()
.HasRequired(s => s.Address)
.WithRequiredPrincipal(ad => ad.Student);
}
上面的代碼中,modelBuilder.Entity<Student>().HasRequired(s => s.Address)
,是設置Student實體中的 StudentAddress導航類型的Address屬性是必須的,.WithRequiredPrincipal(ad => ad.Student)
是設置StudentAddress實體中的Student屬性是必須的。
使用 EF Power Tools爲上面的一對一的例子建立實體對象模型,以下:
這一節,一對一的關係就講解完了,下一節學習一對多的關係。