由于我的数据库设置(我没有控制权)的方式,我有一个外键的情况,我无法弄清楚在实体框架。我正在使用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连接实际上是多对多,每个子节点只有一个父节点)。 –