Studying the pitfalls (if any) of having a DbContext for each ViewModel / Class that uses it

I study the Entity Framework and use it in an MVVM application, where each ViewModel works with a DbContext to access data. Disclaimer: in a real application, I know that the ViewModel should not interact directly with the data access layer.

Given that each ViewModel is designed to monitor and control the state of the view, while maintaining relationships with the models themselves, I began to think about the consequences of rotating several DbContext objects, and if something like DBContext is best left as a singleton, to which I quickly found the answer, NO . "Therefore, if the consensus is to have an instance for each use (as is the case with multiple ViewModels or something-you), I still have not seen where anyone mentions potential problems with this.

To develop, let's say, I have two ViewModels models, and in each I create a Context ( TestContext inherits from DbContext ) to support data access actions throughout the life cycle of each ViewModel:

 public class MainWindowViewModel : ViewModelBase { private TestContext db = new TestContext(); ... other code follows (properties methods etc...)... } public class TestViewModel: ViewModelBase { private TestContext db = new TestContext(); ... other code follows (properties methods etc...)... } 

Are there pitfalls with context in each class that can use it?

One of these thoughts that makes fun of me is that it’s possible that some context doesn’t synchronize with another, so that one ViewModel has more recent data than the others, because its context is more “relevant”. Such things I'm interested in.

Thanks.

EDIT

I do not want to open / cover every situation, as that would be unique to the situation in which you are coding. I just want to know if there are any “upfront” or obvious dangers that I don’t know about being new to this issue.

+7
source share
1 answer

Entity Framework and the DbContext extension support the UnitOfWork design UnitOfWork . The idea is that you keep your logical "transactions" separate. Because of this, you usually want each part or function of your application to process its own instance of DbContext .

The way you can think about it is that the DbContext contains a local copy of what it pulled from the database and keeps track of all the changes made by the user to the local data. When you are ready, you will tell him to return the necessary changes to the database in one go.

For your question about pits and dangers; Entity Framework uses optimized concurrency by default. This means that when saving local changes to the database, concurrency is not checked at all. Everything that you had in the local DbContext is sent back to the database, regardless of whether another user or another context in the application has changed. A great article explaining how and how to change behavior can be found here: http://msdn.microsoft.com/en-us/library/bb738618.aspx

+4
source

All Articles