开发者

Making Object Context (EF4/CTP4) thread safe?

开发者 https://www.devze.com 2023-01-24 02:46 出处:网络
I am using the latest release of entity framework 4 - CTP 4 for my project in ASP.NET 4. This version (EF4-CTP 4) gives an option to build the Object Context (or DBContext) using code first approach.

I am using the latest release of entity framework 4 - CTP 4 for my project in ASP.NET 4. This version (EF4-CTP 4) gives an option to build the Object Context (or DBContext) using code first approach. This approach has its own drawback that its time consuming so I build it up once at the application start. However I read in many articles that it may not be wise to create a singleton object of Object Context. So I wish to maintain small context instance may be per session.

So my questions are: 1. Is it advisable to have Object Context shared accross multiple session? 2. If we wish to have one instance per session can we build it once and use its instance for multiple sessions? 3. If s开发者_C百科hared accross session how can we maintain concurrency/commits?

Thanks.


I would suggest not to use a Singleton. Here's a SO question discussing why.

This also depends on whether this is a web/windows app, but with web apps, we scope the OC to the HttpContext using a DI container.

That way, only one connection is opened per HTTP Request.

0

精彩评论

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

关注公众号