Reputation: 3039
Please, explain when it is nessesary to use db.Entity.Update(x)? Code below works without it.
using (var db = new DBContext())
{
var b = db.Building.First();
b.Residental = true;
db.Building.Update(b); // Still works without this line
await db.SaveChangesAsync();
}
Upvotes: 1
Views: 55
Reputation: 3871
The thing is EF uses changes tracking system for entities. When it is turned on you can just invoke SaveChanges() and it will update tracked entities automatically. But if you disable auto tracking or close the context it will no more be tracking changes, hence won't be able to apply them.
You can see an example of how it's working ouside of the scope of DbContext here.
Entities tracking is very useful thing but it slows down the performance so if you want you can disable it and make Update and another commands manually (which would be the same as set EntityState to Modified/Deleted/etc.). However, be careful if you disable it - there can be problems with lazy loading of EF's proxy types. So if you are on a not very big project and the performance isn't the most important thing for you I would suggest to let entities tracking enabled.
Upvotes: 4