由於我的數據庫設置(我沒有控制權)的方式,我有一個外鍵的情況,我無法弄清楚在實體框架。我正在使用Fluent API來配置我的關係。我的班是非標準的非唯一外鍵
public class Parent
{
// Unique primary key
public int PrimaryKey { get; set; }
// These two fields together compose the foreign key to join to Child
public string ParentForeignKey1 { get; set; }
public string ParentForeignKey2 { get; set; }
public List<Child> Children { get; set; }
}
public class Child
{
// Unique primary key
public int PrimaryKey { get; set; }
// These two fields together compose the (non-unique) foreign key to join to Parent
public string ChildForeignKey1 { get; set; }
public string ChildForeignKey2 { get; set; }
// The parent/child relationship is implicit, even though the tables
// themselves indicate a many-to-many relationship
public List<Parent> Parents { get; set; }
}
實體框架的關係應該是這樣的,兩個表上ParentForeignKey
和ChildForeignKey
領域加入像
SELECT *
FROM Parent
JOIN Child
ON Child.ChildForeignKey1 = Parent.ParentForeignKey1
AND Child.ChildForeignKey2 = Parent.ParentForeignKey2
如何建立良好的API外鍵映射,使實體當我查詢DbSet
時,框架會生成這些連接?
你只有一個家長。你不應該在代碼中有一個名單? –
jdweng
好點。我會更新這個問題。我沒有這樣做,因爲父/子關係是隱含的(即使SQL連接實際上是多對多,每個子節點只有一個父節點)。 –