问题描述
说我有以下POCO类: public class AuditableModel
{
public int ID {get;组; }
public int? SomeOtherModelID {get;组; }
[ForeignKey(SomeOtherModelID)]
public SomeOtherModel SomeOtherModel {get;组; }
public int LastUpdateBy {get;组; }
[ForeignKey(LastUpdateBy)]
public AuditableModel LastUpdateByModel {get;组; }
}
public class SomeOtherModel
{
public int ID {get;组; }
public int LastUpdateBy {get;组; }
[ForeignKey(LastUpdateBy)]
public AuditableModel LastUpdateByModel {get;组; }
}
在这个例子中,我有一个外键 AuditableModel
指向 SomeOtherModel
,外键 SomeOtherModel
指向 AuditableModel
,都是一对多的,都应该是单向的。
当尝试查询我的 AuditableModel
类,我得到无法确定关联的主体结束异常。有两种方法可以解决这个例外:
- 删除
SomeOtherModelID
和SomeOtherModel
上的属性AuditableModel
- 在
SomeOtherModel
在DbContext
的OnModelCreating
方法中使用流畅的API。
我将使用流畅的API实现,但完全可以通过约定和数据注释完成。有没有办法,或者我是通过配置来定义这些关系?
我同意@Eranga。这只适用于Fluent API。这里确切的是,惯例是检测 AuditableModel
中的单个导航属性引用 SomeOtherModel
和单个导航属性在 SomeOtherModel
引用 AuditableModel
。映射惯例尝试在实体之间创建一对一的关系(因为导航属性都不是集合)。因为不清楚什么是校长,而是依赖者得到这个例外。
没有数据注释属性来解决这个问题,即告诉EF实际上是两个关系应该在每个关系上创建一个不暴露的许多方面。您只能在Fluent API中配置(使用不带参数的 WithMany()
overload)。
Say I have the following POCO classes:
public class AuditableModel
{
public int ID { get; set; }
public int? SomeOtherModelID { get; set; }
[ForeignKey("SomeOtherModelID")]
public SomeOtherModel SomeOtherModel { get; set; }
public int LastUpdateBy { get; set; }
[ForeignKey("LastUpdateBy")]
public AuditableModel LastUpdateByModel { get; set; }
}
public class SomeOtherModel
{
public int ID { get; set; }
public int LastUpdateBy { get; set; }
[ForeignKey("LastUpdateBy")]
public AuditableModel LastUpdateByModel { get; set; }
}
In this example, I have a foreign key on AuditableModel
pointing to SomeOtherModel
and a foreign key on SomeOtherModel
pointing to AuditableModel
, both are a one-to-many, both should be unidirectional.
When trying to query my AuditableModel
class, I get the "Unable to determine the principal end of an association" exception. There are two ways I've found to get around this exception:
- Remove the
SomeOtherModelID
andSomeOtherModel
properties onAuditableModel
- Code the relationship on
SomeOtherModel
in theDbContext
'sOnModelCreating
method using the fluent API.
I'll implement using the fluent API, but would have rather done it completely via conventions and data annotations. Is there a way, or am I stuck defining these relationships via configuration?
I agree with @Eranga. This only works with Fluent API. What exactly is going wrong here is that conventions detect a single navigation property on AuditableModel
refering to SomeOtherModel
and a single navigation property on SomeOtherModel
refering to AuditableModel
. Mapping conventions try to create a one-to-one relationship (because neither of the navigation properties is a collection) between the entities. Because it's not clear what's the principal and what the dependent you get this exception.
There is no data annotation attribute to resolve this problem, i.e. to tell EF that actually two relationsships should be created with a not-exposed many-side on each relationship. You can only configure this in Fluent API (using the WithMany()
overload without a parameter).
这篇关于EF代码首先单向一对多与数据注释冲突与其他外键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!