I utilize Data Transfer Objects to move data between the entity framework, the business layer, and the user layer. I'm not sure how to update the right object in the entity framework if I obtain an object that has been transformed to a DTO rather than simply inserting a duplicate.
An EF 4 object that has been generated as a controller parameter in MVC may be updated using the code below from a strongly typed view:
Once the object has been added to the context, it seems that the secret is to utilize the ObjectStateManager to alter the status from Added to Modified.
MyEntities db = new MyEntities(); db.Product.AddObject(product); db.ObjectStateManager.ChangeObjectState(product, System.Data.EntityState.Modified); return db.SaveChanges() > 0;
In accordance with @Sean Mills' remark, use:
((IObjectContextAdapter) db).ObjectContext.ObjectStateManager.ChangeObjectState(entity, System.Data.EntityState.Added);