与实体框架中的代码第一个外键冲突变化 [英] Conflicting changes with code first foreign key in Entity Framework
问题描述
异常来源
public void SaveDomain(IDomain domain){
_entityRepositor = new Donors();
_entityRepositor.DomainReps.Add(new DomainRep(domain));
}
InvalidOperationException
已检测到与Edonics.Repositor.DomainRep_TopLevelDomainRep关系角色DomainRep_TopLevelDomainRep_Target的冲突更改。
外键属性和导航属性应该处于同步状态?
public class DomainRep:IDomain
{
private readonly IDomain _adaptee;
public DomainRep(IDomain adaptee)
{
_adaptee = adaptee;
}
[NotMapped]
public IDomain Adaptee
{
get {return _adaptee; }
}
public string Id
{
get {return _adaptee.Id; }
set {_adaptee.Id = value; }
}
public string TopLevelDomainRepId
{
get {return _adaptee.Tld.Id; }
set {}
}
[ForeignKey(TopLevelDomainRepId)]
public TopLevelDomainRep TopLevelDomainRep
{
get {return new TopLevelDomainRep(Tld); }
set {Tld = value.Adaptee; }
}
public ITopLevelDomain Tld
{
get {return _adaptee.Tld; }
set {_adaptee.Tld = value; }
}
public string SecondLevelDomainRepId
{
get {return _adaptee.Sld.Id; }
set {}
}
[ForeignKey(SecondLevelDomainRepId)]
public SecondLevelDomainRep SecondLevelDomainRep
{
get {return new SecondLevelDomainRep(Sld); }
set {Sld = value.Adaptee; }
}
public ISecondLevelDomain Sld
{
get {return _adaptee.Sld; }
set {_adaptee.Sld = value;
}
}
任何想法?
您的适配器模式会干扰EF的工作原理。您为实体的导航属性提供了不同的实现。例如,如果您访问 TopLevelDomainRep
属性多次,它将返回多个实例,但具有相同的ID /实体密钥。 EF只处理给定主键值的每个上下文的单个实例。
您可以在EF实体之上创建一个域层,或者按预期的方式使用EF实体到。
im using an adapter to be able to plug interfaces rather than concrete types into dbcontext. the adapter works on its own, but im having trouble with getting the any one-to-one (parent child) relationships to be saved into the database:
Exception Source
public void SaveDomain(IDomain domain){
_entityRepositor = new Donors();
_entityRepositor.DomainReps.Add(new DomainRep(domain));
}
InvalidOperationException
Conflicting changes to the role 'DomainRep_TopLevelDomainRep_Target' of the relationship 'Edonics.Repositor.DomainRep_TopLevelDomainRep' have been detected.
The Foreign Key Properties and the Navigation Properties Should Be in Sync?
public class DomainRep: IDomain
{
private readonly IDomain _adaptee;
public DomainRep(IDomain adaptee)
{
_adaptee = adaptee;
}
[NotMapped]
public IDomain Adaptee
{
get { return _adaptee; }
}
public string Id
{
get { return _adaptee.Id; }
set { _adaptee.Id = value; }
}
public string TopLevelDomainRepId
{
get { return _adaptee.Tld.Id; }
set { }
}
[ForeignKey("TopLevelDomainRepId")]
public TopLevelDomainRep TopLevelDomainRep
{
get { return new TopLevelDomainRep(Tld); }
set { Tld = value.Adaptee; }
}
public ITopLevelDomain Tld
{
get { return _adaptee.Tld; }
set { _adaptee.Tld = value; }
}
public string SecondLevelDomainRepId
{
get { return _adaptee.Sld.Id; }
set { }
}
[ForeignKey("SecondLevelDomainRepId")]
public SecondLevelDomainRep SecondLevelDomainRep
{
get { return new SecondLevelDomainRep(Sld); }
set { Sld = value.Adaptee; }
}
public ISecondLevelDomain Sld
{
get { return _adaptee.Sld; }
set { _adaptee.Sld = value; }
}
}
Any ideas?
Your adapter pattern interfere with how EF works. You are providing a different implementation for the navigational properties of the entity. For example, if you access TopLevelDomainRep
property several times it will return multiple instances but with the same ID/Entity Key. EF handles only single instance per context for a given primary key value.
Either you create a domain layer on top of EF entities or you use EF entities the way it was expected to.
这篇关于与实体框架中的代码第一个外键冲突变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!