实体框架中的零或一到零或一个与组合键的关系
问题描述:
我有3个类A,B和AB。 A类和B类相互独立。但AB依赖于A和B.我希望使用实体框架代码优先实现A和B之间的“零或一到零或一个”关系。实体框架中的零或一到零或一个与组合键的关系
有人能告诉我为什么这不起作用吗?还是我完全错了?谢谢!
public class A
{
public int Id { get; set; }
public string PropertyName { get; set; }
public virtual AB AB { get; set; }
}
public class B
{
public int Id { get; set; }
public string PropertyName { get; set; }
public virtual AB AB { get; set; }
}
public class AB
{
[Key, ForeignKey("A"), Column(Order = 0)]
public int AId { get; set; }
[Key, ForeignKey("B"), Column(Order = 1)]
public int BId { get; set; }
public virtual A A { get; set; }
public virtual B B { get; set; }
}
我发现了这样的错误:
AB_B_Source: : Multiplicity is not valid in Role 'AB_B_Source' in relationship 'AB_B'. Because the Dependent Role properties are not the key properties, the upper bound of the multiplicity of the Dependent Role must be '*'.
AB_A_Source: : Multiplicity is not valid in Role 'AB_A_Source' in relationship 'AB_A'. Because the Dependent Role properties are not the key properties, the upper bound of the multiplicity of the Dependent Role must be '*'.
答
基于所编辑的模型中,导航属性AB中的每个类(A和B)可能是问题,因为为指向一个AB对象您需要同时使用两把钥匙。 实体AB可能映射了一个复合主键,并且您想要的关系应该已经使用该实体中的属性A和B来定义。
让我知道它的工作, 有一个愉快的一天
谢谢您的回答。事实上,我一直试图从现有的更复杂的模型中创建一个简化的模型,并且我省略了一些部分。 “GranteeFingerPrintInfo”类实际上是“AB”,应该删除类“A”中属性“Id”的注释。所以注释在真实模型中不是问题。请考虑这些变化,我编辑过mu问题。谢谢! –