//职责:确保一个请求共用一个上下文
public class DataBaseContextFactory
{
//返回基类,这样就可以随意做到切换上下文了
public static DbContext GetDataTabelContext()
{
//共用一个请求上下文实例
DbContext dbcontext = CallContext.GetData("DbContext") as DbContext;
if (dbcontext == null)
{
dbcontext = new Entities();
CallContext.SetData("DbContext", dbcontext);
}
return dbcontext;
}
}
时间: 2024-11-05 21:57:58