In a winform application, the lifespan/scope of context in Entity Framework 4

entity-framework winforms

Question

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.

1
14
4/14/2011 1:10:24 PM

Accepted Answer

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:

Using Unit of Work and Repository patterns with Entity Framework 4 according to MSDN

18
4/14/2011 1:11:58 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow