Abhay Naik
Abhay Naik

Reputation: 420

Making Object Context (EF4/CTP4) thread safe?

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 shared accross session how can we maintain concurrency/commits?

Thanks.

Upvotes: 0

Views: 2163

Answers (1)

RPM1984
RPM1984

Reputation: 73123

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.

Upvotes: 3

Related Questions