WPF Application Global Entity Framework Context

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.

+7
wpf entity-framework
source share
2 answers

A global static context is rarely the right answer. See what happens if the database is reset during the execution of this application - your SQL connection has disappeared and all subsequent queries that use the static context will fail.

We recommend that you find a way to shorten the lifetime for the context of your object - open it, do some work, get rid of it, ...

Regarding placing your different objects in the same EDMX, this is almost certainly the correct answer if they have any relationship between the objects you need in the same EDMX.

As for the reboot, the user should never have this. Behind the scenes, you can open a new context by reloading the current version of the object from the database, applying the changes they made in the user interface, then save it.

You might also want to look at individual objects and be careful with optimistic concurrency exceptions when trying to save changes, and someone else will modify the same object in the database.

+5
source share
Good question, Corey. Lean away from me.

Entity Framework gives you a free choice - you can either initiate several contexts, or have only one, static. It will work well in both cases, and yes, both solutions are safe. The only valuable advice I can give you is to experiment with both, measure performance, delays, etc. And choose the best one for you. It's fun, believe me :)

If this is a really massive application with many concurrent connections, I would recommend using one static context or one static main context and only a few additional ones, only to support the main one. But, since I wrote a few lines above, it meets your requirements, the solution of which is best for you.

I especially liked this part of your question:

I just do not want one of those applications in which the user must reload a dozen times in different parts of the application to receive new data.

WPF is really a really powerful tool, and basically, when users click buttons to update data, they disappear forever. WPF provides you with a truly wide range of asynchronous multi-threaded tools, such as the Dispatcher class or Background worker, for softly updating your desired data in the background. This is really great because you don’t have to worry about pressing various buttons, but also background threads do not block the user interface, so the data is updated transparently from a user point of view.

WPF along with the Entity Framework is really worth the training effort - please feel free to ask if you have any further problems.

+3
source share

All Articles