Entity framework objects should be updated.

entity entity-framework

Question

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.

1
24
12/14/2011 3:30:25 PM

Popular Answer

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);
28
5/31/2013 12:38:09 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow