我使用适配器能够将接口而不是具体类型插入 dbcontext。适配器独立工作,但我无法将任何一对一(父子)关系保存到数据库中:
异常源
public void SaveDomain(IDomain domain){
_entityRepositor = new Donors();
_entityRepositor.DomainReps.Add(new DomainRep(domain));
}
无效操作异常
Conflicting changes to the role 'DomainRep_TopLevelDomainRep_Target' of the relationship 'Edonics.Repositor.DomainRep_TopLevelDomainRep' have been detected.
外键属性和导航属性应该同步吗?
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; }
}
}
有任何想法吗?