如何在身份2.0中使用EF6与通用服务,通用存储库和unityofworkAsync一起使用多个上下文

时间:2015-07-03 10:19:39

标签: entity-framework entity-framework-6 identity

如何在身份2.0中使用EF6与通用服务,通用存储库和unityofworkAsync

我们有BAL,DAL和身份层

////我已经为DbContext

应用了依赖项
public static void RegisterTypes(IUnityContainer container)
        {
            //unit of work.
            container.RegisterType<IUnitOfWorkAsync, UnitOfWork>(new PerRequestLifetimeManager());
            container.RegisterType<IUnitOfWork, UnitOfWork>(new PerRequestLifetimeManager());

        //Set dependancy for unityofwork for both context
        container.RegisterType<IUnitOfWorkAsync, UnitOfWork>(new PerRequestLifetimeManager(), new InjectionConstructor(new ProjectDbContext()));
        container.RegisterType<IUnitOfWorkAsync, UnitOfWork>("Application", new PerRequestLifetimeManager(), new InjectionConstructor(new ApplicationDbContext()));

        container.RegisterType<ICacheManager, MemoryCacheManager>(new PerRequestLifetimeManager());

        //Context number one
        container.RegisterType<DbContext, ProjectDbContext>(new PerRequestLifetimeManager());
        container.RegisterType<IDataContextAsync, ProjectDbContext>(new PerRequestLifetimeManager());

        //Context number two
        container.RegisterType<DbContext, ApplicationDbContext>(new PerRequestLifetimeManager());
        container.RegisterType<IDataContextAsync, ApplicationDbContext>(new PerRequestLifetimeManager());

        //ApplicationDbContext Entity
        container.RegisterType<IRepositoryAsync<RolePermission>, Repository<RolePermission>>(new PerRequestLifetimeManager());
        container.RegisterType<IRepository<RolePermission>, Repository<RolePermission>>(new PerRequestLifetimeManager());
        container.RegisterType<IBaseService<RolePermission>, BaseService<RolePermission>>(new PerRequestLifetimeManager());

        //ProjectDbContext Entity
        container.RegisterType<IRepositoryAsync<NurseryType>, Repository<NurseryType>>(new PerRequestLifetimeManager());
        container.RegisterType<IRepository<NurseryType>, Repository<NurseryType>>(new PerRequestLifetimeManager());
        container.RegisterType<IBaseService<NurseryType>, BaseService<NurseryType>>(new PerRequestLifetimeManager());

        ......
        }

////我的控制器代码 ////我想为_permissionModuleServices获取ApplicationDbContext,但它显示Project dbcontext。 /////我可以为unitOfWork随机获取两个上下文。与我想要的服务相同。我正在使用通用存储库模式。  #region构造函数

    //public RolePermissionController()
    //    : this(new UnitOfWork<ApplicationDbContext>())
    //{
    //}

    /// <summary>
    /// Initializes a new instance of the <see cref="RolePermissionController"/> class.
    /// </summary>
    /// <param name="_rolePermissionService">The _role permission.</param>
    public RolePermissionController(
        IBaseService<PermissionModule> _permissionModuleService,
        IBaseService<RolePermission> _rolePermissionService,
        [Dependency("Application")]IUnitOfWorkAsync _unitOfWork
        )
    {
        this.rolePermissionService = _rolePermissionService;
        this.permissionModuleService = _permissionModuleService;
        this.unitOfWork = _unitOfWork;
    }

    #endregion

0 个答案:

没有答案