由于我的数据库设置方式(我无法控制),我有一个我在实体框架中无法弄清楚的外键情况。我正在使用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
如何设置Fluent API外键映射,以便在查询DbSet
时实体框架生成这些连接?
答案 0 :(得分:0)
我认为这更有意义
public class Parent
{
// The parent/child relationship is implicit, even though the tables
// themselves indicate a many-to-many relationship
public static List<Parent> Parents { get; set; }
// 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; }
}