我一直在努力實現與實體框架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