What is the difference between Attach, setar Entitystate for Modified and Currentvalues.Setvalues?

Asked

Viewed 2,094 times

3

What’s the difference between methods to update a record through the Entityframework?

  1. Attach:

    dbContext.Pessoas.Attach(model);
    
  2. Setar the State from an entrance to EntityState.Modified:

    dbContext.Entry(model).State = System.Data.Entity.EntityState.Modified;
    
  3. And by changing the values of an input by CurrentValues.SetValues:

    dbContext.Entry(entry).CurrentValues.SetValues(model);
    

2 answers

5


Attach

Attach a record to context. It’s an optimistic behavior: the Entity Framework expects the record to exist and only observes other records that make use of this attached record. Any modification made to the attached record is not passed on to the database.

Source: http://msdn.microsoft.com/en-us/data/jj592676.aspx

State = EntityState.Modified

Tells the Entity Framework that the object (hence the record) has been modified and must be persisted in the database when invoking the method SaveChanges.

A priori, records loaded by selection are not marked to be persisted. The programmer must do this manually. It is this way to prevent the SaveChanges make any improper changes in bank in entities that should not be modified.

CurrentValues.SetValues

Directly changes the values and marks the object input as modified. A call to SaveChanges will update the updated values by SetValues.

See more about CurrentValues here.

See more about the SetValues and other methods and properties of CurrentValues here.

1

Another answer to this question is based almost completely on another question that I found in the Soen.

The author of the question was exploring some ways to update a record and among them he listed some:

Method 1 - Load the original record, change each property and then save.

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

The vangatens are to be able to use a Viewmodel, allowing you to have a class without all the properties of the domain class and then facilitate the projection of the View. With this, you need to set the properties you will save, not needing to have all the fields of the domain class in the view and be able to save the change of only the fields you changed.

Already the disadvantages are in:
Have two requests to the database. One to retrieve the original record (record that is in the database with the current data) and the request to persist the data in the database (db.SaveChanges()).

Method 2 - load the original record and make use of db.Entry(T)CurrentValues.SetValues();.

var original = db.Users.Find(updatedUser.UserId);

if (original != null)
{
    db.Entry(original).CurrentValues.SetValues(updatedUser);
    db.SaveChanges();
}

Here, the perks are set to modified properties with only two command lines.
Only the changed properties will be sent to the database.

As disadvantages are on account of your view need to have all properties (here, **updateUser** is not a viewModel, but a class of the )domain itself
Two requests are also made to the database. One to obtain the original record and one to save the record.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.