I am developing a WPF application using Entity Framework (.NET 3.5). He accesses objects in several places. I am concerned about the consistency of the entire application regarding organizations. Do I have to set up individual contexts in my different views or should I (and this is a good way to do this) have an instance of one context that can be accessed globally?
For example, my entity model has three sections: “Shipments” (with child packages and additional child contents), “Companies / contacts” (with child addresses and phones) and disk specifications. The Shipment and EditShipment views access DiskSpecs, and the OptionsView controls DiskSpec (Create, Edit, Delete). If I edit DiskSpec, should I have something in ShipmentsView to get the latest specs, if I have separate contexts correctly?
If it is safe to have one common context from which the rest of the application extracts it, then I think this is the way to go. If so, where would this instance be delivered? I use VB.NET, but I can translate from C # pretty well. Any help would be greatly appreciated.
I just don’t want one of those applications to which the user had to click repeatedly several times in different parts of the application in order to get new data.
Update:
OK, so I changed my application as follows:
- All contexts are created in the "Using Blocks" section to get rid of them when they are no longer needed.
- When loading, all entities are separated from the context prior to its placement.
- A new property in MainViewModel (ContextUpdated) raises an event that all other ViewModels subscribe to which the ViewModels RefreshEntities method fires.
- After that, I started getting errors saying that only one ChangeTracker can refer to an object at a time. Since I could not understand which context was still referencing the object (shouldn't the context be right?), I passed the object as IEntityWithChangeTracker and set SetChangeTracker to nothing (Null).
This solved the current problem: When I Null change the Tracker to Entity, and then attach it to the context, it loses its changed state and is not updated in the database. However, if I do not reset the change tracker, I cannot connect. I have my own change tracking code, so this is not a problem.
My new question is: how should you do this. Good example. The essence of the request and the code-saving code could go a long way, because I hit my head trying to get what I once thought was a simple transaction.
CodeWarrior
source share