ASP - Core Migrate EF Core SQL DB on Startup

后端 未结 11 948
故里飘歌
故里飘歌 2020-12-01 02:32

Is it possible to have my ASP Core Web API ensure the DB is migrated to the latest migration using EF Core? I know this can be done through the command line, but I want to

11条回答
  •  忘掉有多难
    2020-12-01 03:12

    This code works in .NET core 3.0

     using (var scope = app.ApplicationServices.CreateScope())
     {
         var dbContext = scope.ServiceProvider.GetService();
         dbContext.Database.Migrate();
     }
    

提交回复
热议问题