I have an MVC3 project that is using an EF4 project as its Domain. The Domain is a Model first project that uses T4 to build POCO objects. There are several ComplexTypes at play in the Domain and everything works great as long as I use the proxies returned by context.CreateObject().
When an MVC3 action is called, the model binder passes a non-proxy object that contains the changes to be applied to the Domain.
I want to work with the "proxy'ed" original so the views have access to navigation properties later, so a straight up AttachTo doesn't cut it.
I need to get the "original" proxy'ed object from the context first, then update it with the changes contained in the POCO provided by the model binder.
From what I've read, and my research tells me, I should be able to accomplish this using something like the following:
public static T GetUpdatedProxy<T>(this ObjectContext context, string entitySetName, T entity)
where T : class
{
object original; // db original POCO, proxy wrapped.
var entityKey = context.CreateEntityKey(entitySetName, entity);
//Load DB object
context.GetObjectByKey(entityKey, out original)
//Apply changes from binder supplied POCO object.
context.ApplyCurrentValues<T>(entitySetName, entity); //<= error here
return (T) original;
}
My issue is this error:
[InvalidOperationException: The entity of type 'System.Data.Entity.DynamicProxies.Value_E954C24C522BA1D4124F434A57391656EFA4DD7CEFFD3A5CE35FC1532CD1B10A' references the same complex object of type 'Domain.DateRange' more than once. Complex objects cannot be referenced multiple times by the same entity.]
System.Data.Objects.EntityEntry.CheckForDuplicateComplexObjects(Object complexObject) +418
System.Data.Objects.EntityEntry.DetectChangesInProperties(Boolean detectOnlyComplexProperties) +211
System.Data.Objects.Internal.EntityWithChangeTrackerStrategy.UpdateCurrentValueRecord(Object value, EntityEntry entry) +93
System.Data.Objects.Internal.EntityWrapper`1.UpdateCurrentValueRecord(Object value, EntityEntry entry) +17
System.Data.Objects.EntityEntry.ApplyCurrentValuesInternal(IEntityWrapper wrappedCurrentEntity) +107
System.Data.Objects.ObjectContext.ApplyCurrentValues(String entitySetName, TEntity currentEntity) +365
- The complex object on the non-proxy object and the one on the proxy'ed object are not the same.
- The entity only has the one complex object so it can't be set multiple times to say, two properties of the same ComplexType.
- The complex object itself hasn't actually had any values set to it so the two null-able fields are in fact still null.
- If I do use the AttachTo method and then set the object state to modified the save works but I can't later use the object to return a view because the navigation properties are null.
Any thoughts? I appreciate the help.