【发布时间】:2016-05-19 11:15:23
【问题描述】:
我有这样的 EF6 Code First 模型设计。从Another 类知道谁是家长(ParentA 或ParentB)的最佳方法是什么。谢谢!
public class Child
{
public int Id { get; set; }
public List<Another> AnotherList { get; set; }
}
public class Another
{
public int Id { get; set; }
public int ChildId { get; set; }
public Child Child { get; set; }
public string AnotherName { get; set; }
}
public class ParentA
{
public int Id { get; set; }
public int ChildId { get; set; }
public Child Child { get; set; }
}
public class ParentB
{
public int Id { get; set; }
public int ChildId { get; set; }
public Child Child { get; set; }
}
public class Context : DbContext
{
public DbSet<ParentA> ParentA { get; set; }
public DbSet<ParentB> ParentB { get; set; }
public DbSet<Child> Child { get; set; }
public DbSet<Another> Another { get; set; }
}
【问题讨论】:
-
Child需要一个ParentA和一个ParentB属性,将其变成两个 1:1 关联。 I 另一种方法称为多态关联,但如果不是绝对必要,我不会这样做。
标签: c# .net entity-framework entity-framework-6 relationship