Entity framework does a great job of detecting when a field changes on any object that it is tracking. These changes will be automatically saved when SaveChanges is executed. As I discussed in Why the Repository Pattern I like to create an example Generic Class that is type T where T is a Model or a User in my Entity Framework Context. This example will extend upon those context query to retrieve the modified entry.
Tag: savechanges
-
Detaching specific objects using Entity Framework before SaveChanges
Sometimes in large processes that span across multiple classes, it’s possible that an entity is added to Entity Framework’s context tracking. I’ve run into this scenario where we accidentally save things that get immediately deleted afterwards.
Let’s look at a neat solution that will allow you to detach specific objects out of Entity Framework’s context. In theory this can go in several places: In the code you know causes the entities to be added or before the call to SaveChanges. In this example we’ll look at the latter because it is a nice and generic spot.