When working with the EntityObject derived types or change-tracking proxy objects, the Entity Framework automatically tracks changes made to entities as they occur
What advantage does this give me? Without this my changes are still detected when I call开发者_JS百科 SaveChanges and my POCO is persisted correctly.
Also, why do a lot of the online tutorials for EF explicitly change the state to modified after they make a change, what purpose does this serve?
context.Entry(model).State = EntityState.Modified;
EF tracks changes of your entities, so that when you call SaveChanges() it will know which entities to update in database, i.e. - what SQL to generate and run against db.
The reason for having below line, is to attach a model which is currently not being tracked and set its state to modified.
context.Entry(model).State = EntityState.Modified;
You need to do it in case if you created an instance of your Entity yourself, e.g. -
var customer = new Customer();
This will not add your customer to the DbContext and therefore its not being tracked. So you need to use context.Entry(customer) for that.
精彩评论