I am using Entity Framework 4.3.1 from an ASP.NET MVC 3.0 web application, and I have some code that separates the old object and replaces it with a new object that looks like this:
public void UpdateUnattached(T entryToUpdate, T updatedEntry)
{
var ctx = (EntityFrameworkContext) _context;
ctx.ChangeObjectState(EntityState.Detached, entryToUpdate);
_set.Attach(updatedEntry);
ctx.ChangeObjectState(EntityState.Modified, updatedEntry);
}
This works all the time, except when I get an exception below in the line _set.Attach.
You cannot return this RelationshipManager. The RelatedEnd function can only be returned by the MaterialManager for objects that are tracked by the ObjectStateManager or implement IEntityWithRelationships.
, , , , , - (, , ). , ...
, ?
try/catch GetValidationErrors catch, .
(InnerException is null)...
Type: System.InvalidOperationException
Source: System.Data.Entity
Target site: GetRelatedEndInternal
Stacktrace: at System.Data.Objects.DataClasses.RelationshipManager.GetRelatedEndInternal(String relationshipName, String targetRoleName)
at System.Data.Objects.EntityEntry.FindRelatedEntityKeysByForeignKeys(Dictionary`2& relatedEntities, Boolean useOriginalValues)
at System.Data.Objects.EntityEntry.TakeSnapshotOfForeignKeys()
at System.Data.Objects.ObjectStateManager.PromoteKeyEntryInitialization(EntityEntry keyEntry, IEntityWrapper wrappedEntity, IExtendedDataRecord shadowValues, Boolean replacingEntry)
at System.Data.Objects.ObjectContext.AttachSingleObject(IEntityWrapper wrappedEntity, EntitySet entitySet, String argumentName)
at System.Data.Objects.ObjectContext.AttachTo(String entitySetName, Object entity)
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.Attach(Object entity)
at System.Data.Entity.DbSet`1.Attach(TEntity entity)
at ... snip ...