We are switching from ObjectContext to DbContext in your EF6 desktop application. At the moment I struggle with reloading data from the DB without recreating the whole context. Unfortunately we work partly with a long living context approach which I can't change easily.
To reload the properties of an entity (without the navigation properties) with the newest values from the database (store wins), we use CreateObjectSet() with MergeOption.OverwriteChanges from the ObjectContext. I am now trying to replace it with a DbContext alternative.
I have found the following options:
DbContext.Entry(entity).Reload()- Overwrite the values of each property in the entity using the values from
DbContext.Entry(entity).GetDatabaseValues() - Detaching the entity and fetch it again e.g. using
Find()
Even when disabling AutoDetectChanges all these options are still much slower as when using CreateObjectSet(). By the way: Creating a new context and fetch the values again would be slower too but not that much.
Am I missing something or does DbContext not have a performant alternative (apart from creating a new context) for CreateObjectSet() with StoreWins?