EF 用CallContext上下文管理

时间:2023-03-08 17:13:11
EF 用CallContext上下文管理
public class ObjectContextFactory
{
private static CIK_NewsEntities context;
public static DbContext GetContext()
{
//从CallContext数据槽中获取EF上下文
context = CallContext.GetData(typeof(ObjectContextFactory).FullName) as CIK_NewsEntities;
if (context==null)
{
//如果CallContext数据槽中没有EF上下文,则创建EF上下文,并保存到CallContext数据槽中
context = new CIK_NewsEntities();
CallContext.SetData(typeof(ObjectContextFactory).FullName, context);
}
return context;
} }