views:

29

answers:

2

I've been checking my application with linq 2 sql profiler, and I noticed that it opens a lot of datacontexts, most of them are opened by the linq datasource I used, since my repositories use only the instance stored in Request.Items, is it bad to open too many datacontext? and how can I make my linqdatasource to use the datacontext that I store in Request.Items for the duration of the request? thanks for any help!

+1  A: 

DataContexts are intended to have short lifetime. Opening many of them is not necessarily a bad thing. A good rule of thumb is that each "unit of work" should have its own DataContext.

See this article for more details.

It is better to err on the side of shorter lifetime

Mark Byers
A: 

If you open and close them, absolutely not.

But if you're inside a TransactionScope, the game changes! Opening and closing many nested DataContexts bit us hard because it caused our transactions to promote to distributed transactions, which slowed down our system quite a bit and severely cut our system's scalability.

Dave Markle