简体   繁体   English

使对象上下文(EF4 / CTP4)线程安全吗?

[英]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. 我在ASP.NET 4中为项目使用最新版本的实体框架4-CTP4。此版本(EF4-CTP 4)提供了使用代码优先方法构建对象上下文(或DBContext)的选项。 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? 所以我的问题是:1.是否建议跨多个会话共享对象上下文? 2. If we wish to have one instance per session can we build it once and use its instance for multiple sessions? 2.如果我们希望每个会话有一个实例,我们可以一次构建一个实例并将其实例用于多个会话吗? 3. If shared accross session how can we maintain concurrency/commits? 3.如果是跨会话共享,我们如何保持并发/提交?

Thanks. 谢谢。

I would suggest not to use a Singleton . 我建议不要使用Singleton Here's a SO question discussing why . 这是一个SO问题,讨论原因

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. 这也取决于这是否是Web / Windows应用程序,但是对于Web应用程序,我们使用DI容器将OC限定在HttpContext范围内。

That way, only one connection is opened per HTTP Request. 这样,每个HTTP请求仅打开一个连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM