我设置我的DbContext建模after a post here on stackoverflow在这里找到。混淆静态数据库上下文
这是当前设置...
public static class DbContext
{
public static MyDbContext Db
{
get
{
if (!HttpContext.Current.Items.Contains("_db"))
{
HttpContext.Current.Items.Add("_db", new MyDbContext());
}
return HttpContext.Current.Items["_db"] as MyDbContext;
}
}
}
的背景设置在Global.asax的上END_REQUEST像这样:
void Application_EndRequest(object sender, EventArgs e)
{
var db = (MyDbContext)HttpContext.Current.Items["_db"];
if (db != null)
db.Dispose();
}
这样一来,在我的系统,我可以访问数据库像DbContext.Db.xxxx
到目前为止,一切都在我本地运行良好,但是,我没有在生产环境中与多个用户进行测试。
吾言...
I read this post on stackoverflow,现在有我担心可能有多个用户访问静态上下文数据的问题。这应该关注我还是以我的方式设置好?
+1在这里,这绝对是好的。这是'Items'容器的用途。 –
+1,“静态上下文”根本不是静态的。相反,静态方法用于实例化或返回当前HTTP请求的数据库上下文。 –