I am using Entity Framework 6 where, for performance reasons, I load my entities into my DbContext
up front and then use them locally. Up to now, all changes to the database have gone through the DbContext
so my local entities and the database have been in sync. However, I now have to call a Stored Procedure on the database, which has the side effects of making changes to tables (outside of the DbContext) that need to be reflected in my entities. By changes, I mean it is adding new records and deleting / updating existing records.
I do not want to dispose of my DbContext
and create a new one, as some of the entity instances are wrapped within ViewModel
classes. So, deleting the DbContext
in this way would lead to major problems in the UI.
It is my understanding that simply calling Load()
on all my DbSets
of the DbContext
will just replace the existing instances. So, any objects using the old entities instances won't work.
So, I thought I could use the Reload
method like:
context.Entry(entity).Reload();
which would update my local entities, but I can only do this for the entities that the DbContext
already knows about. It doesn't cover any NEW entities or DELETED entities that were created / deleted as a result of the Stored Procedure executing.
So, I am looking for a way to:
- Load, from the database, entities that are NEW to my
DbContext
- Reload existing entities in my
DbContext
- Remove any deleted entities from my
DbContext