I apologize for asking yet another question concerning the longevity of an EF4 context, but I've been pondering this for a while and can't seem to find a solution. I want to keep it basic since I don't know a lot of patterns or (in my opinion) very sophisticated things.
I work with an ASP.NET application where each http request manages the context, which is, in my view, a really smart technique.
But now that I'm using a winforms application, I sometimes have transactions or reports that won't run well if I just build the context for each query. Not that this speed issue is really troubling, but I'm curious whether there's a straightforward approach similar to an HTTP request in ASP.NET for winforms.
Avoid establishing a context for each query. Don't, however, construct a context that is utilized for a Form's whole existence (or your application).
For each Unit of Work, provide its context. (which can encompass many queries).
In this method, you can let Entity Framework handle the database requests in a cute little transaction while keeping all of your modifications contained inside the context.
In order to utilize the Repository and Unit of Work patterns separately, I choose to isolate them from the Entity Context (which makes everything very clear). In fact, MSDN has a good article on the specifics: