EF 一個實體對象不能由多個 IEntityChangeTracker 實例引用 解決辦法
在DAL層中,建立工廠類
public static class SysDbContextFactory
{
///
/// 從Http上下文中獲取EF容器
///
///
public static SysDbContext GetSysDbContext()
{
var context = HttpContext.Current.Items[nameof(SysDbContext)] as SysDbContext;
if (context == null)
{
context = new SysDbContext();
HttpContext.Current.Items[nameof(SysDbContext)] = context;
}
return context as SysDbContext;
}
}
然後在DAL 傳入
public class ProductTypeService:EntityHelper, IProductTypeService
{
public ProductTypeService() : base(SysDbContextFactory.GetSysDbContext()) // 這裏
{}
public ProductTypeService(SysDbContext sysDbContext) : base(sysDbContext)
{
}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.