Multiple DbContext

Nov 13, 2012 at 2:18 PM

Hi,

I'm using your library extensively and I've ran into a stubbling block. I'm trying to get the repository and unit of work to work with multiple DbContext with Unity IoC that's in my project.

If you can point out where and how do I configure in such a way I can use multiples DbContext?

Thanks.

Jan 29, 2013 at 3:15 PM

I also have a requirement in my project to utilize multiple DbContexts, and would like to know how to register my contexts with Unity IoC container. If I understand it correctly, as is, we'd need to register the DbContext to resolve to a single specific context.

Any help with this would be appreciated.

Thanks!

May 4, 2013 at 5:42 PM
Hi, I need to address this problem too, please can you give us an answer for this request.
May 4, 2013 at 7:11 PM
Edited May 4, 2013 at 7:13 PM
I hope this could help anyone:
   IUnityContainer container = new UnityContainer();

            container.RegisterType<DbContext, CompanyContext>("CompanyContext")
                     .RegisterInstance("CompanyContextAdapter",
                                       new DbContextAdapter(container.Resolve<DbContext>("CompanyContext")),
                                       new ContainerControlledLifetimeManager())
                     .RegisterType<IObjectSetFactory>("CompanyObjectSetFactory",
                                                      new InjectionFactory(
                                                          con => con.Resolve<DbContextAdapter>("CompanyContextAdapter")))
                     .RegisterType<IObjectContext>("CompanyOjectContext",
                                                   new InjectionFactory(
                                                       con => con.Resolve<DbContextAdapter>("CompanyContextAdapter")))
                     .RegisterType<IRepository<RM30101>, Repository<RM30101>>(
                         new InjectionConstructor(container.Resolve<IObjectSetFactory>("CompanyObjectSetFactory")))
                     .RegisterType<INotasDeCreditoService, NotasDeCreditoService>();
In this example I just have registered one dbcontext but since its named, if I register another the resolution process would have a mechanism to discriminate.
Jun 7, 2013 at 6:03 AM
Edited Jun 7, 2013 at 6:05 AM
I had troubles with my last configuration for multiples DbContexts. This one is probed and is working. Enjoy :)
var container = new UnityContainer();

container.RegisterType(typeof(DbContext)
                                 , typeof(FirstContext)
                                 , "firstContext");
container.RegisterInstance("firstContext"
                                     , new DbContextAdapter(container.Resolve<DbContext>("firstContext"))
                                     , new ContainerControlledLifetimeManager());

container.RegisterType(typeof(IRepository<>)
                                 , typeof(Repository<>)
                                 , "firstContext"
                                 , new InjectionConstructor(container.Resolve<DbContextAdapter>("firstContext")));
container.RegisterType(typeof(IUnitOfWork)
                                 , typeof(UnitOfWork)
                                 , "firstContext"
                                 , new InjectionConstructor(container.Resolve<DbContextAdapter>("firstContext")));

container.RegisterType(typeof(DbContext)
                                 , typeof(SecondContext)
                                 , "secondContext");
container.RegisterInstance("secondContext"
                                     , new DbContextAdapter(container.Resolve<DbContext>("secondContext"))
                                     , new ContainerControlledLifetimeManager());

container.RegisterType(typeof(IRepository<>), typeof(Repository<>), "secondContext"
              , new InjectionConstructor(container.Resolve<DbContextAdapter>("secondContext")));
container.RegisterType(typeof(IUnitOfWork)
                                 , typeof(UnitOfWork)
                                 , "secondContext"
                                 , new InjectionConstructor(container.Resolve<DbContextAdapter>("secondContext")));


var repository1 = container.Resolve<IRepository<Foo>>("firstContext");
var uow1 = container.Resolve<IUnitOfWork>("firstContext");

var repository2 = container.Resolve<IRepository<Foo>>("secondContext");
var uow2 = container.Resolve<IUnitOfWork>("secondContext");