Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update entity framework objects

Tags:

I transfer data between the entity framework and the business layer and user layer by using Data Transfer Objects. I do have some doubt, if I retrieve an object which is converted to a DTO, how do I update the correct object in the entity framework and not just insert a duplicate?

like image 265
user75278 Avatar asked Mar 08 '09 14:03

user75278


People also ask

How do you update an object in Entity Framework?

Update Objects in Entity Framework 4.0 The steps to update an existing entity are quite simple. First retrieve an instance of the entity from the EntitySet<T> (in our case ObjectSet<Customer>), then edit the properties of the Entity and finally call SaveChanges() on the context.

How do you refresh a table in Entity Framework?

Use the Refresh method: context. Refresh(RefreshMode. StoreWins, yourEntity);


2 Answers

The following code will update an EF 4 entity that has been created as a controller parameter in MVC from a strongly typed view:

It seems the trick is to use the ObjectStateManager to change the state from Added to Modified once the entity has been added to the context.

MyEntities db = new MyEntities(); db.Product.AddObject(product); db.ObjectStateManager.ChangeObjectState(product, System.Data.EntityState.Modified); return db.SaveChanges() > 0; 

As per @Sean Mills comment if you are using EF5 use:

 ((IObjectContextAdapter) db).ObjectContext.ObjectStateManager.ChangeObjectState(entity, System.Data.EntityState.Added); 
like image 160
Rob Avatar answered Sep 30 '22 13:09

Rob


an old question, but just in case someone needs a code solution:

http://www.mikesdotnetting.com/Article/110/ASP.NET-MVC-Entity-Framework-Modifying-One-to-Many-and-Many-to-Many-Relationships

Example:

public void EditArticle(         Article article, string articleTypeId, string[] categoryId)  {    var id = 0;    Article art = de.ArticleSet                    .Include("ArticleTypes")                   .Include("Categories")                   .Where(a => a.ArticleID == article.ArticleID)                   .First();    var count = art.Categories.Count;   for (var i = 0; i < count; i++)   {     art.Categories.Remove(art.Categories.ElementAt(i));     count--;   }   foreach (var c in categoryId)   {     id = int.Parse(c);     Category category = de.CategorySet         .Where(ct => ct.CategoryID == id).First();     art.Categories.Add(category);   }    art.Headline = article.Headline;   art.Abstract = article.Abstract;   art.Maintext = article.Maintext;   art.DateAmended = DateTime.Now;    art.ArticleTypesReference.EntityKey = new EntityKey(                                           "DotnettingEntities.ArticleTypeSet",                                            "ArticleTypeID",                                            int.Parse(articleTypeId)                                           );    de.SaveChanges(); } 
like image 42
Bart Avatar answered Sep 30 '22 14:09

Bart