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.
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.