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.