InvalidOperationException on Find if primary key calculated using child foreign keys

[ Using Code First DbContext with Entity Framework 5.0 RC ]

Entity with Id Derived from its Navigational Properties
public class Domain
{
    private string _id;
    private SecondLevelDomain _secondLevelDomain;
    private TopLevelDomain _topLevelDomain;

    public string Id
    {
        get
        {
            // Trigger setter synthesis
            Id = null;
            return _id;
        }
        set
        {
            string parentId = String.Empty;
            if (Sld.Id != null)
                output += Sld.Id + " ";
            if (Tld.Id != null)
                output += Tld.Id;
            _id = parentId;
        }
    }

    public string SecondLevelDomainId
    {
        get;
        set;
    }

    [ForeignKey("SecondLevelDomainId")]
    public SecondLevelDomain Sld
    {
        get 
        { 
            return _secondLevelDomain 
            ?? (_secondLevelDomain = new SecondLevelDomain());
        }
        set 
        { 
            Debug.WriteLine("Foreign Setter Not Called Before Its Too Late");
            _secondLevelDomain = value;
        }
    }

    public string TopLevelDomainId
    {
        get;
        set;
    }

    [ForeignKey("TopLevelDomainId")]
    public TopLevelDomain Tld
    {
        get 
        { 
             return _topLevelDomain 
             ?? (_topLevelDomain = new TopLevelDomain()); 
        }
        set { _topLevelDomain = value; }
    }
}
Parent Id Evaluates to Empty on Domain Creation from Database
public CheckDomainInDatabase(string domainId) {
    var domainFromDatabase = Repositor.Domains.Find(domainId);
}
  • InvalidOperationException: The value of a property that is part of an object's key does not match the corresponding property value stored in the ObjectContext. This can occur if properties that are part of the key return inconsistent or incorrect values or if DetectChanges is not called after changes are made to a property that is part of the key.

I need to be able to retrieve these domains with aggregate identifiers, because I need to then modify some of their other properties (not shown) - but this error is throwing me off that track...

Answers


You cannot change key of existing attached entity. Once the entity is inserted its key is fixed - it can never change. If you need to change the key of attached entity you must create clone of that entity and insert it as a new one.

The main rule of entity in entity framework is that it must be uniquely identifiable and this identification must not change. Your class is not valid entity for entity framework.

Btw. what you created is same as using Sld.Id and Tld.Id as composite primary key of your entity which is at least clear to everybody who check your entity model or database schema.


Need Your Help

Set index on a table or a view?

sql tsql

I am testing couple of stored procs in SQL Man. studio and I received couple of suggestions on where and how to add indexes on a relevant table.

How to watch a variable defined in a service in angularjs

angularjs

I am trying to watch changes on an json array defined in an angularj service, but when the change occures, the $watch function is not firing. My controller and service code goes as follows (plunker...