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.
Isaiah nelson
source share