I have been experiencing the ObjectContext inconsistent state exception:
The changes to the database were committed successfully, but an error occurred while updating the object context. The ObjectContext might be in an inconsistent state. Inner exception message: AcceptChanges cannot continue because the object's key values conflict with another object in the ObjectStateManager. Make sure that the key values are unique before calling AcceptChanges.
This seems to happen when my base entity (abstract since using TPC) has an
public int Id { get; set;}
which is configured as a primary key generated by the database:
// Base Entity
modelBuilder.Entity<BaseEntityObject>().HasKey(t => t.Id);
modelBuilder.Entity<BaseEntityObject>().Property(t => t.Id).
HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
and derived entities are also inheriting this and trying to use it as their primary key.
When you instantiate two different types of derived entries, add them to the DbContext, then try to SaveChanges(), the exception is fired. However, when the Id field is changed to a Guid from an int:
public Guid Id { get; set;}
this exception is no longer thrown.
Can someone explain why this happens when the Id property is of type int?
Is there a workaround to this? It seems a bit wasteful to have every single derived entity to have a Guid PK.
IDcolumn marked asPrimaryKeyand set toIdentitycolumn in database, and if it is, does it haveEntityKeyproperty set totruein the EDMX model editor? - Robert