1

I want to update database on Azure SQL to fit .Net Core application using Entity Framework Core migrations. The following command is advised online:

Update-Database -ConnectionString $ConnectionString -ConnectionProviderName "System.Data.SqlClient"

However, it does not work since Update-Database cmdlet does not recognize parameter –ConnectionString. How can automate database migrations?

user2341923
  • 4,537
  • 6
  • 30
  • 44

1 Answers1

1

How can automate database migrations?

EF does not support Automatic migrations, you may need to manually execute Add-Migration or dotnet ef migrations add for adding migration files. You could explicitly execute the command to apply the migrations, also you could apply migrations in your code.

If you want to apply migration at runtime, you could add the following code int the Configure method of Startup.cs

using (var scope = app.ApplicationServices.GetService<IServiceScopeFactory>().CreateScope())
    {
        scope.ServiceProvider.GetRequiredService<ApplicationDbContext>().Database.Migrate();
    }

For more details, you could refer to this thread.

Joey Cai
  • 18,968
  • 1
  • 20
  • 30