Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework 5 Updating a Record

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

  • Can specify which properties change
  • Views don't need to contain every property

Cons

  • 2 x queries on database to load original then update it

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

  • Only modified properties are sent to database

Cons

  • Views need to contain every property
  • 2 x queries on database to load original then update it

Method 3 - Attach updated record and set state to EntityState.Modified

db.Users.Attach(updatedUser); db.Entry(updatedUser).State = EntityState.Modified; db.SaveChanges(); 

Pros

  • 1 x query on database to update

Cons

  • Can't specify which properties change
  • Views must contain every property

Question

My question to you guys; is there a clean way that I can achieve this set of goals?

  • Can specify which properties change
  • Views don't need to contain every property (such as password!)
  • 1 x query on database to update

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 ;-)

like image 498
Stokedout Avatar asked Mar 11 '13 10:03

Stokedout


People also ask

How do I update a record in Entity Framework Core?

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.

How do I update table data in Entity Framework?

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 do you update an entity?

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.

How do I update Entity Framework view?

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.


2 Answers

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(); 
like image 83
Ladislav Mrnka Avatar answered Oct 01 '22 14:10

Ladislav Mrnka


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.

like image 34
smd Avatar answered Oct 01 '22 14:10

smd