Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update only modified values (EntityFramework 5.0)?

I have this entity, want to update using entityframework

 EmployeeModel employee = new EmployeeModel
    {
        Id = 1000, //This one must 
        FirstName = modifiedValue,
        Email = modifiedValue, 
        LastName = originalValue,
        Phone = originalValue
    };

Code to update

_db.ObjectStateManager.ChangeObjectState(employee, EntityState.Modified);  
_db.SaveChanges();

This is the SQL statement got once updated

Update Employee set Id=1138,FirstName='modifiedValue',Email='modifiedValue',LastName= 'OriginalValue',phone='originalValue' where Id=1138

But I am expecting this

Update Employee set FirstName='modifiedValue', Email='modifiedValue' where Id=1138.

I dont know what I am missing here. Please let me know.

like image 586
sivaL Avatar asked Jan 31 '13 14:01

sivaL


People also ask

How do I update my EF record?

We can update records either in connected or disconnected scenarios. In the connected Scenario, we open the context, query for the entity, edit it, and call the SaveChanges method. In the Disconnected scenario, we already have the entity with use. Hence all we need to is to attach/add it to the context.


2 Answers

This problem is common when dealing with DTOs. An employee entity is fetched from the database, mapped to a DTO and sent over the wire. The client then modifies this DTO and sends it back to the server.

When you touch (set) a property on an EF entity, EF will assume that the value has been changed. Even if the old value and the new value are exactly the same. The same problem occurs when you map the DTO to a new Entity and attach it to EF and updating its status to 'Modified'.

Using AutoMapper:

// This will result in the full update statement
var employee = AutoMapper.Mapper.Map<EmployeeDto, Employee>(dto);

// This will result in a smaller update statement (only actual changes)
var employee = dbContext.Employees.Find(dto.Id);
AutoMapper.Mapper.Map(dto, employee);

Or, manually (I would avoid doing this, but just for the sake of completeness):

// This will result in a smaller update statement (only actual changes)
var employee = dbContext.Employees.Find(dto.Id);
if (employee.Email != dto.Email )
    employee.Email = dto.Email;

There are probably some other ways for dealing with this problem... but using AutoMapper together with Entity Framework correctly is definitely one of the easiest ways.

like image 174
deherch Avatar answered Sep 19 '22 07:09

deherch


This is the solution I got

 var entity = _db.CreateObjectSet<Employee>();
 entity.Detach(employee);
 entity.Attach(employee);

 foreach (string modifiedPro in employeeModel.ModifiedProperties){
  _db.ObjectStateManager.GetObjectStateEntry(employee).SetModifiedProperty(modifiedPro);}

  _db.SaveChanges();

Only modified values in the sql update statement

Update Employee set FirstName='modifiedValue', Email='modifiedValue' where Id=1138.

If anybody knows better answer than this, Please post your suggestions

like image 37
sivaL Avatar answered Sep 19 '22 07:09

sivaL