我一直在努力实现与实体框架4.1,它实例上的Application_BeginRequest中的DbContext一个新的MVC3项目中的任何问题,并配置它Application_EndRequest是否有与使用静态属性返回的DbContext
protected virtual void Application_BeginRequest()
{
HttpContext.Current.Items["_EntityContext"] = new EntityContext();
}
protected virtual void Application_EndRequest()
{
var entityContext = HttpContext.Current.Items["_EntityContext"] as EntityContext;
if (entityContext != null)
entityContext.Dispose();
}
该的EntityContext类的定义如下:
public class EntityContext : MyEntities, IDisposable
{
**//should this be static?**
public static EntityContext Current
{
get { return HttpContext.Current.Items["_EntityContext"] as EntityContext; }
}
void IDisposable.Dispose()
{
Current.Dispose();
}
我的问题是,将定义我目前的财产为静态造成任何问题在多用户的情况?
相关http://stackoverflow.com/questions/6987908/what-is-the-best-way-to-instantiate-and-dispose-dbcontext-in-mvc/6990244#6990244 – Eranga 2012-04-10 00:28:48