Skip to content

Instantly share code, notes, and snippets.

@aaronolds
Created September 29, 2017 14:13
Show Gist options
  • Save aaronolds/9591c96a9a429ffc3258b231cad832d6 to your computer and use it in GitHub Desktop.
Save aaronolds/9591c96a9a429ffc3258b231cad832d6 to your computer and use it in GitHub Desktop.
Detach all entities in a DbContext before loading it again with data. The only issue I have with this is, that the detached entities are still in memory.
...
public void DetatchAllEntities()
{
ChangeTracker.Entries().Where(dbEntityEntry => dbEntityEntry.Entity != null).ToList().ForEach(e => e.State = System.Data.Entity.EntityState.Detached);
}
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment