C# 实体框架,列表列表是否可能?
我想知道在C#中使用EF 6.0是否可以实现以下功能 所以我有一个实体A,它有一个dict,类型B的实体作为键,C的实体列表作为值:C# 实体框架,列表列表是否可能?,c#,database,entity-framework,C#,Database,Entity Framework,我想知道在C#中使用EF 6.0是否可以实现以下功能 所以我有一个实体A,它有一个dict,类型B的实体作为键,C的实体列表作为值: public class A { public int AID { get; set; } public Dictionary<B, List<C>> myList { get; set; } public somePrimitive AdditionalPropertyOfA { get; set; }
public class A
{
public int AID { get; set; }
public Dictionary<B, List<C>> myList { get; set; }
public somePrimitive AdditionalPropertyOfA { get; set; }
public somePrimitive AnotherPropertyOfA { get; set; }
}
所以我希望能弄清楚我的意思。在上面的示例中,我们将有两个A实体、两个B实体和四个C实体
对于A=1,我们有:
B=1:1->2和B=2:3->4
对于A=2,我们有:
B=1:1和B=2:2->3->4
问题是,我不知道如何在实体类A中或使用fluent Api编写它。类A需要有一个到实体B的n:m映射,也需要一个到C的n:m映射。但是如何将这些东西结合起来呢
编辑:
为了更清楚地说明我的意思:将A视为工作计划,B视为员工,C视为员工的任务。B本身是实体(名称、地址等),但在A的上下文中,B已分配任务。此设计可能更像下面的示例。你还没有给出任何理由去编一本字典,所以我不知道你为什么要用它
public class A
{
public int AID { get; set; }
public somePrimitive AdditionalPropertyOfA { get; set; }
public somePrimitive AnotherPropertyOfA { get; set; }
public virtual ICollection<B> MyBs { get; set; }
}
public class B
{
public int BID { get; set; }
public int AID { get; set; }
public virtual ICollection<C> MyCs { get; set; }
}
public class C
{
public int CID { get; set; }
public int BID { get; set; }
}
即使这是可能的。这是可怕的数据库设计。大多数程序员/DBA都致力于更高的标准形式。这连1NF都没有!可能您可以使用类文件到表的自定义映射。你可以使用fluent API。那么这里有什么更好的主意呢?不要对实体a使用普通数据库,而只使用XML文件。或者规范化它,使代码中的类出现更多问题?顺便问一下,这种情况下1NF是多少?我如何用Fluent API实现自定义映射?谢谢你的想法。但是现在我有一个问题,如果我要求一个B对象,这个B对象有一个myc集合。但只有从A获得附加信息后,B才应该连接到C。将A视为工作计划,B视为员工,C视为员工的任务。B是独立的实体(名称、地址等),但在A的上下文中,B已分配任务。
public class A
{
public int AID { get; set; }
public somePrimitive AdditionalPropertyOfA { get; set; }
public somePrimitive AnotherPropertyOfA { get; set; }
public virtual ICollection<B> MyBs { get; set; }
}
public class B
{
public int BID { get; set; }
public int AID { get; set; }
public virtual ICollection<C> MyCs { get; set; }
}
public class C
{
public int CID { get; set; }
public int BID { get; set; }
}
Table A
AID AdditionalPropertyOfA ...
1 ...
2 ...
Table AB (or AtoB)
AID BID
1 1
1 2
2 1
2 2
Table B
BID
1
2
Table BC (or BtoC)
BID CID
1 1
1 2
2 2
2 3
2 4
Table C
CID
1
2
3
4