I want to update database schema on production database. Usually in my development I just do Add-Migration and Update-Database.
My question is how to generate script so I can manually read through the query before executing in production environment? I've tried Update-Database -script but it gave me No pending explicit migrations.
Try running:
Update-Database -Script -SourceMigration: <initial-migration> -TargetMigration: <latest-migration>
Obviously replace <initial-migration> and <latest-migration> with the appropriate names of those migrations.
That should generate a script of all migrations at once.
More information can be found here on MSDN.
Related
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?
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.
I have enabled migrations. I am not using automatic migrations and I do not want to use them.
I have done 3 migrations.
In my development environment I am updating database via VS using the command
Update-Database -verbose -StartUpProjectName EntityFrameworkContext -TargetMigration <MigrationName>
In my devlopment environment, everything works correctly.
Now, I must update the database in production environment. I am using following command
Update-Database -verbose -StartUpProjectName EntityFrameworkContext -TargetMigration <MigrationName> -script
I run the generated script on production database. No error. The table __MigrationHistory looks to be good (I have just some doubt about the column Model that is different from dev environment). In this tabel I have the correct number of rows and the column MigrationId is correctly filled.
The database is exactly the same of the dev database. I have checked it.
I have updated also the program.
But, I do not understand why I still have always the same error:
The model backing the 'PublicAreaContext' context has changed since the database was created. Consider using Code First Migrations to update the database (http://go.microsoft.com/fwlink/?LinkId=238269).
I am going crazy.
Thank you for your help
Using Entity Framework 6, with a SQL Server database and code first migrations, I have migrations set up and running "happily", however I'm finding that every time I go to add another migration I get the following error first off:
Unable to generate an explicit migration because the following explicit migrations are pending: [201602090629398_Initial, 201602090638322_FixSitePageColumns, 201602110313468_RemoveRequiredTemplateId]. Apply the pending explicit migrations before attempting to generate a new explicit migration."
If I run update-database -force it complains (naturally) that the script fails because of already having been applied. A strange combination of doign this again and then the add-migration works and I can carry on updating to the latest migration.
The list of "pending" migrations also grows with every addded migration over time.
What could cause this "pending" state, when the migration HAS already been applied? I can confirm that the database has already been updated, the migration exists in __MigrationHistory, and the program is running along happily.
EDIT
I've also just run get-migrations and the following is being returned:
PM> get-migrations
Retrieving migrations that have been applied to the target database.
201602110313468_RemoveRequiredTemplateId
201602110311536_RemoveRequiredTemplateId
201602090638322_FixSitePageColumns
201602090629398_Initial
The weird thing here (possibly a red herring) is that 201602110311536_RemoveRequiredTemplateId is not a migration in my project, I think it may have been one that I created and then deleted, I didn't ever implicitly apply it to the database but it's in the __MigrationHistory table.
This happened to me because when running Add-Migration it couldn't connect to the database to see what migrations had been applied. I don't have my password stored in the connection string. The fix was to run Add-Migration specifying a complete -ConnectionString and -SqlProvider
Add-Migration MigrationName -verbose -ConnectionString "data source=foo.database.windows.net;initial catalog=FOO_DB;user id=admin;password=`"REDACTED`";MultipleActiveResultSets=True;App=EntityFramework" -ConnectionProviderName "System.Data.SqlClient"
Remove the entry for removerequiredtemplateid from the migrations database table and rerun your migration procedure.
Optionally, delete the table altogether, delete all of your migrations, rerun enable-migrations, create a new initial migration and run update-database again (basically, create a fresh start).
Remember to make the required backups beforehand.
I am very, very new to using migrations, but based on the answer to the SO question Database is not getting created at first time, using the Powershell command Update-Database -Script should give me a script to execute outside of automatic migrations. Yet when I try running that command, I get an error message that says:
Unable to update database to match the current model because there are
pending changes and automatic migration is disabled
I would rather follow Chris Pratt's sage advice - in his answer to the linked question - and leave automatic migrations disabled, but the alternative he offers is insisting on automatic migrations.
I am trying to create the database from scratch, using the CreateDatabaseIfNotExists initializer.
It means there are changes to your current model that haven't been added to a migration.
Try:
Add-Migration YourMigrationName
Update-Database -Script
This will first create a migration with the updated model changes, then you can generate the update database SQL script.
I have two database each with their own dbcontext. I've setup two migration configurations. I can add a migration for the first db ust fine (Add-Migration DB1_InitialCreate -ConfigurationTypeName DB1Configuration). When I try to create an initial migration with the second db using: Add-Migration DB2_InitialCreate -ConfigurationTypeName DB2Configuration, I get the following error:
Unable to generate an explicit migration because the following explicit migrations are pending: [201205082215265_DB1_InitialCreate]. Apply the pending explicit migrations before attempting to generate a new explicit migration.
So I do what it says and update the database with:
Update-Database -ConfigurationTypeName DB1Configuration
Then I try to add a migration again for the second db but I keep getting the same error.
Any ideas on how to get migrations working for two databases/contexts?
I've been able to answer my own question. My two configuration classes existed in the same namespace. As soon as I separated them, everything worked.
With Entity Framework 6 it's easy.
It's the same procedure for both multiple DbContexts for one database and for multiple DbContexts for each their own database:
Enable-Migrations -ContextTypeName <DbContext-Name-with-Namespaces> -MigrationsDirectory:<Migrations-Directory-Name>
Add-Migration -configuration <DbContext-Migrations-Configuration-Class-with-Namespaces> <Migrations-Name>
Update-Database -configuration <DbContext-Migrations-Configuration-Class-with-Namespaces> -Verbose
Source
It's been a while, but this could be a better answer to your problem :)
Update-Database -ConfigurationTypeName "SlaveConfiguration"
-StartupProjectName "FacturatieMVCv2.Data" -Verbose
-ConnectionString "connstring;"
-ConnectionProviderName "System.Data.SqlClient"