1

How to use multiple context in identity 2.0 using EF6 with generic services, generic repository and unityofworkAsync

We have BAL, DAL and Identity Layer

////I have applied dependency for both 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());

        ......
        }

////My Controller Code ////I want to get ApplicationDbContext for _permissionModuleServices but its display Project dbcontext. /////I can get both context randomly for unitOfWork. Same as I want for services. I am using generic repository pattern. #region Constructor

    //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
Eldho
  • 7,795
  • 5
  • 40
  • 77
Bhavsar Jay
  • 163
  • 1
  • 6
  • May help this [ASP.NET Identity with Repository and Unit of Work](http://stackoverflow.com/questions/23226140/asp-net-identity-with-repository-and-unit-of-work) – Eldho Jul 03 '15 at 11:02
  • i am facing this issue.. the entity type is not part of the current context in generic repository.. help me – Bhavsar Jay Jul 04 '15 at 11:21
  • Please add the code for more precise information . I assuming that you are trying to access identity context from repository context, this may not work . `IdentityContext` is not part of your default `DbContext` – Eldho Jul 06 '15 at 11:04
  • i was updated my code. Help me – Bhavsar Jay Jul 07 '15 at 06:11
  • I have done code for multiple context in identity but when i going to save record it shows me correct context but don't save in database. Without any error. – Bhavsar Jay Jul 08 '15 at 04:52

0 Answers0