Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why use Attach for update Entity Framework 6?

While searching for the best practivies of performing CRUD operation via EF I noticed that it is highly recommended to use Attach() or Find() methods before updating an entity. It works well and according to EF documentation these methods fetch the entity to context that is quite clear for me. But the followind code confused me pretty much

public void Update(object entity)
{
    Record record = new Record() {
        id = 1,
        value = 5
    };
    using (SomeContext ctx = new SomeContext())
    {
        ctx.Entry(record).State = EntityState.Modified;
        ctx.SaveChanges();
    }
}

Assume we have a record with id = 1 in database. On this condition the code above will update the record (set the value to 5). The question is why it works? And then why should I use Attach()?. As far as I understand the record wasn't attached to context in any way. I read relevant chapters of this book and the tutorial but they use 2-query-approach. Also I surfed SO but didn't find answer on my question. Help me with explanation or some good matherials, please.

like image 331
Vitalii Isaenko Avatar asked Dec 07 '16 19:12

Vitalii Isaenko


People also ask

Why use Entity Framework attach?

Attach is used to repopulate a context with an entity that is known to already exist in the database. SaveChanges will therefore not attempt to insert an attached entity into the database because it is assumed to already be there.

What is the primary difference between the add and attach methods on a DbContext?

Add is to indicate to the Entity Framework that we are going to want to insert a new record in the database. In contrast, Entry and Attach are used in scenarios where the record already exists in the database, and we simply want to make some kind of modification on it. var attach = context.

How do I update data in Entity Framework?

Update Objects in Entity Framework 4.0First 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.


1 Answers

If you have an entity that you know already exists in the database but which is not currently being tracked by the context - which is true in your case - then you can tell the context to track the entity using the Attach method on DbSet. So in summary what Attach method does is track the entity in the context and change its state to Unchanged. When you modify a property after that, the tracking changes will change its state to Modified for you. In the case you expose above you are telling explicitly that state is Modified but also to attach the entity to your context. You can find a detailed explanation in this post.

When should you use Attach method?

When you have an entity that you know already exists in the database but want to make some changes:

var entity= new Entity{id=1};
context.YourDbSet.Attach(entity); 

// Do some change...  
entity.value=5;

context.SaveChanges(); 

This is the same:

 context.Entry(entity).State = EntityState.Unchanged; 

// Do some change... 
entity.value=5; 

context.SaveChanges(); 

When should you change entity's State to Modified explicitly?

When you have an entity that you know already exists in the database but the changes have already been made then. The same scenario of your example

like image 79
octavioccl Avatar answered Oct 30 '22 20:10

octavioccl