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:
DbContext
DbContext
DbContext
Here is the official documentation for Entity Framework.
Starting from the analysis of your database situation, it suggests smart and quick ways to obtain what you want, detailing when necessary data-read strategies (like eager or lazy loading) or providing tutorials to correctly use code generation and the Wizard GUI.
http://www.entityframeworktutorial.net/choosing-development-approach-with-entity-framework.aspx
Here some more detailed info and tutorial on data-read strategies:
https://www.c-sharpcorner.com/article/eager-loading-lazy-loading-and-explicit-loading-in-entity-framework/
As I already told you in comments, I would suggest a database-first approach and with lazy loading to avoid uncontrolled data behaviours (or reloading the whole db when running a stored procedure).
Talking about the SP, it can simply be mapped through the Wizard that comes with Entity Framework and wrapped by a method.
Hope you will find these resources helpful!