InvalidOperationException on DbContext Add Entity RRS feed

  • Question

  • I just changed my Entity Framework code generation to POCO using DbContext. I use 2 ways to add a new entity:

    1: context.Addresses.Add(dbAddress);

    (gives exception for several every entity types, see below)

    2: shipment.Addresses.Add(dbAddress);

    (works ok)

    (context is DbContext instance, shipment is an entity associated to Address with a referential constraint on a 2 column primary key: nId : Int32, nCompany : Byte)

    dbAddress is created like this:

     Address address = new Address();
                address.nId = nId;
                address.nCompany = nCompanyId;
                address.bSequence = sequenceid;

    (address primary key is nId, nCompany, bSequence )

    The address primary key is generated by code, not the database.

    Using method 2 :

    InvalidOperationException: The property 'nCompany' is part of the object's key information and cannot be modified.

    Stacktrace :

       at System.Data.Objects.EntityEntry.VerifyEntityValueIsEditable(StateManagerTypeMetadata typeMetadata, Int32 ordinal, String memberName)
       at System.Data.Objects.EntityEntry.GetAndValidateChangeMemberInfo(String entityMemberName, Object complexObject, String complexObjectMemberName, StateManagerTypeMetadata& typeMetadata, String& changingMemberName, Object& changingObject)
       at System.Data.Objects.EntityEntry.EntityMemberChanging(String entityMemberName, Object complexObject, String complexObjectMemberName)
       at System.Data.Objects.EntityEntry.EntityMemberChanging(String entityMemberName)
       at System.Data.Objects.ObjectStateEntry.System.Data.Objects.DataClasses.IEntityChangeTracker.EntityMemberChanging(String entityMemberName)
       at System.Data.Objects.Internal.SnapshotChangeTrackingStrategy.SetCurrentValue(EntityEntry entry, StateManagerMemberMetadata member, Int32 ordinal, Object target, Object value)
       at System.Data.Objects.Internal.EntityWrapper`1.SetCurrentValue(EntityEntry entry, StateManagerMemberMetadata member, Int32 ordinal, Object target, Object value)
       at System.Data.Objects.DataClasses.EntityReference.UpdateForeignKeyValues(IEntityWrapper dependentEntity, IEntityWrapper principalEntity, Dictionary`2 changedFKs, Boolean forceChange)
       at System.Data.Objects.DataClasses.RelatedEnd.Add(IEntityWrapper wrappedTarget, Boolean applyConstraints, Boolean addRelationshipAsUnchanged, Boolean relationshipAlreadyExists, Boolean allowModifyingOtherEndOfRelationship, Boolean forceForeignKeyChanges)
       at System.Data.Objects.ObjectStateManager.PerformAdd(IEntityWrapper wrappedOwner, RelatedEnd relatedEnd, IEntityWrapper entityToAdd, Boolean isForeignKeyChange)
       at System.Data.Objects.ObjectStateManager.PerformAdd(IList`1 entries)
       at System.Data.Objects.ObjectStateManager.DetectChanges()
       at System.Data.Objects.ObjectContext.DetectChanges()
       at System.Data.Entity.Internal.InternalContext.DetectChanges(Boolean force)
       at System.Data.Entity.Internal.Linq.InternalSet`1.ActOnSet(Action action, EntityState newState, Object entity, String methodName)
       at System.Data.Entity.Internal.Linq.InternalSet`1.Add(Object entity)
       at System.Data.Entity.DbSet`1.Add(TEntity entity)

    Friday, October 5, 2012 8:31 AM


  • Hello, I think you have a problem with the relationship between Addresses and Companies. It seems that nCompanyId is a foreign key. To set it, depending in the POCO generation tool you can use the Company navigation property of the Address entity instead of setting the foreign key directly.



    • Proposed as answer by Alexander Sun Tuesday, October 9, 2012 2:14 AM
    • Marked as answer by Alexander Sun Tuesday, October 23, 2012 8:31 AM
    Friday, October 5, 2012 8:41 AM