Conflicting changes with code first foreign key in Entity Framework

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?

Answers


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.


Need Your Help

What is equivalent of moq.As in JustMock?

c# .net entity-framework moq justmock

I am stuck to mock Entity Framework 6 asynchronous methods using JustMock. I searched in Google a lot but did not get any sufficient result. At last I got an example at testing with async queries b...

How to pass a Troposphere object attribute conditionally?

python amazon-web-services troposphere

I'm using Troposphere to build CloudFormation stacks and would like to pass the Elastic Load Balancer ConnectionSettings attribute only if it's set in my configuration, otherwise I don't want to sp...