0

我在理解實體框架代碼優先關係創建時遇到問題,因爲我更習慣於傳統方式。實體框架 - 關係混淆

一對多的關係對我來說似乎很清楚:子女只需要一個foreignKey ID屬性來指示他們屬於哪個父母。

public class Parent 
{ 
    public int Id { get; set; } 
    public virtual ICollection<Child> Childs { get; set; } 
} 

public class Child 
{ 
    public int Id { get; set; } 

    public int ParentId { get; set; } 
    public virtual Parent Parent { get; set; } 
} 

現在,我不太清楚如何正確地創建一個許多一對多關係。可能需要額外的表ParentChild,因此不需要(外鍵)ID屬性?現在

public class Parent 
{ 
    public int Id { get; set; } 
    public virtual ICollection<Child> Childs { get; set; } 
} 

public class Child 
{ 
    public int Id { get; set; } 
    public virtual ICollection<Parent> Parents { get; set; } 
} 

,爲一到一個關係,我不知道。 public class Parent { public int Id {get;組; }

public int ChildID { get; set; } 
    public virtual Child child { get; set; } 
} 

public class Child 
{ 
    public int Id { get; set; } 

    public int ParentId { get; set; } 
    public virtual Parent Parent { get; set; } 
} 

是國外ID性能,即使需要或者我可以只是在ParentChild屬性和Parent型物業在Child類?當我省略外鍵ID屬性時,允許使用virtual關鍵字嗎?

回答

0

我建議你看看實體框架中的流暢api。一對一的關係可以通過流暢的API輕鬆實現。 Explanation source。快速參考:

public class Student 
    { 
     public Student() { } 

     public int StudentId { get; set; } 
     [Required] 
     public string StudentName { get; set; } 

     [Required] 
     public virtual StudentAddress StudentAddress { get; set; } 

    } 


    public class StudentAddress 
    { 
     [Key, ForeignKey("Student")] 
     public int StudentId { 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; } 
    } 

您可以在您的datacontext類中重寫OnModelCreating。

protected override void OnModelCreating(DbModelBuilder modelBuilder) 
    { 
     modelBuilder.Entity<StudentAddress>() 
      .HasKey(e => e.StudentId); 
     modelBuilder.Entity<StudentAddress>() 
        .Property(e => e.StudentId) 
        .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None); 
     modelBuilder.Entity<StudentAddress>() 
        .HasRequired(e => e.Student) 
        .WithRequiredDependent(s => s.StudentAddress); 

     base.OnModelCreating(modelBuilder); 
    } 
0

使用實體框架,您甚至不必指定外鍵關係,因爲它會從您的模型中推導出來並相應地創建表格。唯一的關係類型您實際上需要做的是0..1或1到0..1或1.

不要忘記,對象模型比數據庫模型更寬鬆。您可以將屬性存儲在對象中,但不能存儲在表中。

因爲EF會在數據庫級別爲您完成工作,所以您必須有不同的思考方式,並且您可以訪問對象模型中定義的所有屬性,甚至集合屬性。

我總是用它來完成它的規則如下:

如果關係的基數爲0..1或1,使用參照其他實體對象爲你的財產。如果基數很多,請使用集合。

下面是一些用例:

1對多(每父許多孩子的):

public class Parent 
{ 
    public int Id { get; set; } 
    // Navigation property 
    public virtual ICollection<Child> Childs { get; set; } 
} 

public class Child 
{ 
    public int Id { get; set; } 
    // Navigation property 
    public virtual Parent Parent { get; set; } 
} 

結果數據庫將是一個父表與單個屬性(ID)和表具有兩個屬性的子表,Id和外鍵屬性自動生成名爲Parent_Id(表名然後是下劃線,然後是相關類的關鍵屬性)。

多對多:

public class ClassA 
{ 
    public int Id { get; set; } 
    // Navigation property 
    public virtual ICollection<ClassB> ClassBs { get; set; } 
} 

public class ClassB 
{ 
    public int Id { get; set; } 
    // Navigation property 
    public virtual ICollection<ClassA> ClassAs { get; set; } 
} 

結果在數據庫將是一個表ClassA的具有單個屬性(ID)的表格ClassB的具有單個屬性(ID)和一個第三表(該關係表中的多對多關係)與兩個屬性(這兩個表的Ids)。

EF會推斷出爲了完成這項工作需要什麼,所以你不必再那麼具體。

現在的唯一有點問題之一,1至1:

public class ClassA 
{ 
    public int Id { get; set; } 
    // Navigation property 
    public virtual ClassB ClassB { get; set; } 
} 

public class ClassB 
{ 
    public int Id { get; set; } 
    // Navigation property 
    public virtual ClassA ClassA { get; set; } 
} 

繼我一開始給了規則,這是我們會做。但是在這種情況下,EF無法知道關係的方向...... 1對1的結果是任一方向。我們將不得不讓它知道使用註釋的方向(對我來說,與Fluent API相比,這是最簡單的方法)。

public class ClassA 
{ 
    public int Id { get; set; } 
    // Navigation property 
    public virtual ClassB ClassB { get; set; } 
} 

public class ClassB 
{ 
    [ForeignKey("ClassA")] 
    public int Id { get; set; } 
    // Navigation property 
    public virtual ClassA ClassA { get; set; } 
} 

註釋[ForeignKey的( 「ClassA的」)],在ClassB的講述EF從ClassB的使用ID列作爲ClassA的外鍵。

數據庫中的結果將是具有2個屬性(Id和ClassB_Id)的Table ClassA和具有單個屬性(Id)的表ClassB。

您不必自己創建foreigh key屬性,因爲EF將爲您執行此操作。