How do you configure the DbContext when creating Migrations in Entity Framework Core?

后端 未结 7 1299
时光取名叫无心
时光取名叫无心 2020-12-11 02:51

Is there way that dependency injection can be configured/bootstrapped when using Entity Framework\'s migration commands?

Entity Framework Core supports dependency in

7条回答
  •  半阙折子戏
    2020-12-11 03:18

    To combine the answers above this works for me

    private readonly bool isMigration = false;
    public MyContext()
    {
        isMigration = true;
    }
    
    public MyContext(DbContextOptions options) : base(options)
    {
    
    }
    
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        if (isMigration)
        {
            optionsBuilder.UseSqlServer("CONNECTION_STRING");
        }
    }
    

提交回复
热议问题