Automatically execute migrations when publishing ASP.NET Core app

后端 未结 4 1183
难免孤独
难免孤独 2020-12-16 23:19

Question

Is there any ways that I can automatically execute the migration code (EF 7) when publishing my ASP 5 application to IIS using Web Deploy?<

4条回答
  •  执念已碎
    2020-12-16 23:54

    Use context.Database.Migrate()

    You can call this from your Startup class:

    using (var context = new MyContext(...))
    {
        context.Database.Migrate();
    }
    

    It will migrate your database to the latest version on application startup. But be careful doing it, maybe comment out this code and uncommend only when you want to run your migrations.

提交回复
热议问题