EF。 AutoFac。不断得到错误DBContext已被处置

时间:2016-04-13 12:48:46

标签: asp.net-mvc entity-framework c#-4.0 ef-code-first autofac

初始数据。 Code First,Autofac

之前Linq2Sql和整个数据层都在主项目中。 我在EF CodeFirst上重写了所有内容。 现在我一直收到错误:DBContext已被处理

有一个课程:

public class DataManager: IDataManager    
{
    private IModelDataContextWrapper _dataContext;

    public IAuditRepository Audits { get; set; }
    public ICDMRepository CDMs { get; set; }
    public ICommandRepository Commands { get; set; }
    public IMonitoringRepository Monitorings { get; set; }
    public IRoleRepository Roles { get; set; }
    public ISettingRepository Settings { get; set; }
    public IUserRepository Users { get; set; }

    public DataManager(IModelDataContextWrapper dataContext)    
    {    
        _dataContext = dataContext;    
    }

    public IModelDataContextWrapper DataContext    
    {    
        get    
        {    
            return _dataContext;    
        }    
    }

    public void Dispose()    
    {    
        if (_dataContext != null)    
        {    
            _dataContext.Dispose();    
            _dataContext = null;    
        }
        GC.SuppressFinalize(this);    
    }

    public void SaveChanges()    
    {    
        try    
        {    
            DataContext.SaveChanges();    
        }    
        catch (Exception ex)    
        {    
            string err = string.Format("exception: {0} stacktrace: {1}", ex.Message, ex.StackTrace);

            Logger.Instance.WriteLine(err);    
        }    
    }    
}

public class UserRepository : IUserRepository    
{    
    private readonly IModelDataContext _context;

    public UserRepository(IModelDataContext context)    
    {    
        _context = context;    
    }
}

Autofac注册:

var builder = new ContainerBuilder();
builder.RegisterControllers(Assembly.GetExecutingAssembly());

builder.RegisterType<ModelDataContext>().As<IModelDataContext>().InstancePerLifetimeScope();
builder.RegisterType<ModelDataContextWrapper>().As<IModelDataContextWrapper>().InstancePerLifetimeScope();
builder.RegisterType<ArchiveModelDataContext>().As<IArchiveModelDataContext>().InstancePerLifetimeScope();

builder.RegisterType<DataManager>().As<IDataManager>().PropertiesAutowired().InstancePerLifetimeScope();

builder.RegisterType<AuditRepository>().As<IAuditRepository>().InstancePerLifetimeScope();
builder.RegisterType<CommandRepository>().As<ICommandRepository>().InstancePerLifetimeScope();
builder.RegisterType<CDMRepository>().As<ICDMRepository>().InstancePerLifetimeScope();
builder.RegisterType<MonitoringRepository>().As<IMonitoringRepository>().InstancePerLifetimeScope();
builder.RegisterType<RoleRepository>().As<IRoleRepository>().InstancePerLifetimeScope();
builder.RegisterType<SettingRepository>().As<ISettingRepository>().InstancePerLifetimeScope();
builder.RegisterType<UserRepository>().As<IUserRepository>().InstancePerLifetimeScope();
builder.RegisterType<ThemedViewEngine>();

builder.RegisterFilterProvider();

IContainer container = builder.Build();

DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
var lifetimeScope = DependencyResolver.Current.GetService<ILifetimeScope>();

lifetimeScope.InjectProperties(Membership.Provider);

lifetimeScope.InjectProperties(Roles.Provider);

有什么问题?

1 个答案:

答案 0 :(得分:0)

我个人使用.InstancePerRequest();在网络环境中。 您需要在视图中避免IQueryable,确保在控制器中有ToList()或请求触发器,而不是在视图中

更新:

        builder.RegisterType<ModelDataContextWrapper>()
            .As<IModelDataContextWrapper >()
            .InstancePerRequest();