管理ADO.NET实体框架的ObjectContext的ASP.NET MVC [英] Manage ADO.NET Entity Framework ObjectContext in ASP.NET MVC
问题描述
我使用在MVC应用程序ADO.NET EF。我正在考虑把里面的ObjectContext的HttpContext.Current,使在同一请求中的所有逻辑可以访问它,而不必打开/每次销毁。不过,我真的知道,如果它是一个管理ObjectContext的实例的好方法。我有2个关于这方面的需求问题:
-
由于HttpContext.Current属性是由一个线程局部场和支持ASP.NET使用线程池从处理请求,这可能是一个ObjectContext的实例,由请求投入HttpContext.Current将是可见的从池中同一线程上运行的后续请求?
-
您如何看待ObjectContext的应在ASP.NET MVC中进行管理,以避免双方地段开/部署和prevent竞争条件?
我同意托德 - 使用DI / IoC的cotnainer(团结,温莎),每个线程(或自定义每个请求)的寿命
2的广告,我记得在LINQ to SQL中,DataContext对象被认为是一个轻量级的对象,因此它不应该是一个问题,经常创建它。我们希望,这是EF相似。
I'm using ADO.NET EF in an MVC application. I'm considering putting the ObjectContext inside HttpContext.Current so that all logic in the same request can access to it without having to open/destroy each time. However, I'm really sure if it's a good way to manage ObjectContext instances. I have 2 questions regarding this need:
As HttpContext.Current property is backed by a thread-local field and ASP.NET uses threads from pool to handle requests, is it possible that an ObjectContext instance put into HttpContext.Current by a request will be visible to a subsequent request running on the same thread from the pool?
How do you think ObjectContext should be managed in ASP.NET MVC to both avoid lots of opening/disposing and prevent race conditions?
I agree with Todd - use DI/IoC cotnainer (Unity, Windsor) with per-thread (or custom per-request) lifetime.
Ad 2, as I remember, in Linq to SQL, DataContext object was considered a lightweight object so it should not be a problem to create it often. Hopefully, it is similar for EF.
这篇关于管理ADO.NET实体框架的ObjectContext的ASP.NET MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!