Automatically execute migrations when publishing ASP.NET Core app

后端 未结 4 1182
难免孤独
难免孤独 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:52

    In you Startup.cs class add this code

     public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            using (var serviceScope = app.ApplicationServices.GetRequiredService().CreateScope())
            {
                var context = serviceScope.ServiceProvider.GetService(); 
                context.Database.Migrate(); 
            }
        }
    

提交回复
热议问题