Multiple DbContext, multiple Database.SetInitializer

前端 未结 3 1419
北恋
北恋 2021-01-02 14:34

I have created two DbContexts, one is for application configuration, the second is for logging.

The reason being, I want to set a maximum size on the logging db so

3条回答
  •  爱一瞬间的悲伤
    2021-01-02 14:55

    Yes you can do it. You just need to initialize before you move on to the next one.

      Database.SetInitializer(myInitializer);
      MyDbContext context = new MyDbContext();
      context.Database.Initialize(false);
    
      Database.SetInitializer(myInitializer);
      MySecondDbContext context2 = new MySecondDbContext();
      context2.Database.Initialize(false);
    

    Note: that I usually get the DbContext instance from a dependency resolver...

提交回复
热议问题