开发者

Is opening too many datacontexts bad?

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!


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜