I have been exploring different methods of editing/updating a record within Entity Framework 5 in an ASP.NET MVC3 environment, but so far none of them tick all of the boxes I need. I'll explain why.
I have found three methods to which I'll mention the pros and cons:
Method 1 - Load original record, update each property
var original = db.Users.Find(updatedUser.UserId); if (original != null) { original.BusinessEntityId = updatedUser.BusinessEntityId; original.Email = updatedUser.Email; original.EmployeeId = updatedUser.EmployeeId; original.Forename = updatedUser.Forename; original.Surname = updatedUser.Surname; original.Telephone = updatedUser.Telephone; original.Title = updatedUser.Title; original.Fax = updatedUser.Fax; original.ASPNetUserId = updatedUser.ASPNetUserId; db.SaveChanges(); }
Pros
Cons
Method 2 - Load original record, set changed values
var original = db.Users.Find(updatedUser.UserId); if (original != null) { db.Entry(original).CurrentValues.SetValues(updatedUser); db.SaveChanges(); }
Pros
Cons
Method 3 - Attach updated record and set state to EntityState.Modified
db.Users.Attach(updatedUser); db.Entry(updatedUser).State = EntityState.Modified; db.SaveChanges();
Pros
Cons
Question
My question to you guys; is there a clean way that I can achieve this set of goals?
I understand this is quite a minor thing to point out but I may be missing a simple solution to this. If not method one will prevail ;-)
First, we create a new context and retrieve the existing department data from the database. We modify the Descr field. Since the context is open, it will mark the entity as Modified . Finally, when we call the SaveChanges , the context generates the update SQL statement to persist the change to the database.
To update this entity we need to attach the Department to the context and inform it to mark its status as Modified . Now if we call the SaveChanges method, the context will send an update query to the database.
How to Update a new record in to a existing entity using update entity action? First you need to check the id if id exist then you can use the update entity action and if id is new then you can used the create entity action.
We can handle views in entity framework in two ways. Create a view combining multiple tables in the database manually and subsequently add an entity for the view. Finally, we can add ignore for the entity OnmodelCreating Entity builder, as shown below.
You are looking for:
db.Users.Attach(updatedUser); var entry = db.Entry(updatedUser); entry.Property(e => e.Email).IsModified = true; // other changed properties db.SaveChanges();
I really like the accepted answer. I believe there is yet another way to approach this as well. Let's say you have a very short list of properties that you wouldn't want to ever include in a View, so when updating the entity, those would be omitted. Let's say that those two fields are Password and SSN.
db.Users.Attach(updatedUser); var entry = db.Entry(updatedUser); entry.State = EntityState.Modified; entry.Property(e => e.Password).IsModified = false; entry.Property(e => e.SSN).IsModified = false; db.SaveChanges();
This example allows you to essentially leave your business logic alone after adding a new field to your Users table and to your View.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With