Entity Framework Core on Azure: Update-Database parameter –ConnectionString does not work

azure azure-sql-database database-migration entity-framework entity-framework-core

Question

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?

1
0
6/19/2018 5:56:24 AM

Accepted Answer

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.

1
6/19/2018 9:50:46 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow