我設置我的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請求的數據庫上下文。 –