I have a system in production which was created with Entity Framework 4.1 Code First. Now, I have upgraded to 4.3 and need to apply migrations, but there's several use cases I need to cover:
Seed()
method also applies some unique indices.)How do I create the migrations and an initializer (or initializers) to cover both these cases?
Run the Add-Migration InitialCreate command in Package Manager Console. This creates a migration to create the existing schema. Comment out all code in the Up method of the newly created migration. This will allow us to 'apply' the migration to the local database without trying to recreate all the tables etc.
Step 1 − Before running the application you need to enable migration. Step 2 − Open Package Manager Console from Tools → NuGet Package Manger → Package Manger Console. Step 3 − Migration is already enabled, now add migration in your application by executing the following command.
Migrations are enabled by default in EF Core. They are managed by executing commands. If you have Visual Studio, you can use the Package Manager Console (PMC) to manage migrations. Alternatively, you can use a command line tool to execute Entity Framework CLI commands to create a migration.
Since your production database was created with EF 4.1, you'll need to do a bit of work to get it ready for use with Migrations. Start with a copy of your current production code running in a dev environemnt. Make sure the dev database doesn't exist.
Upgrade the project to use EF 4.3 (or later) with Migrations and create the initial migration to snapshot what production currently looks like.
Update-Package EntityFramework
Enable-Migrations
Add-Migration InitialCreate
Replace your database initializers with matching Migrations code.
For seed data, add it to the Seed()
method of the Migrations\Configuration.cs
file. Note that unlike the Seed()
method in initializers, this method gets run every time Update-Database
is called. It may need to update rows (reset the seed data) instead of inserting them. The AddOrUpdate()
method can aid with this.
Since your unique indecies can now be created with Migrations, you should add them to the Up()
method of the InitialCreate migration. You can either chain them off the CreateTable()
calls using the Index()
method, or by calling the CreateIndex()
method.
You can use the MigrateDatabaseToLatestVersion initializer now to run Migrations during initialization.
Get a script to bootstrap your production environment.
Update-Database -Script
From the script that gets generated, you'll want to delete almost everything since the tables aready exist. The parts you'll need are the CREATE TABLE [__MigrationHistory]
and INSERT INTO [__MigrationHistory]
statements.
Optionally, drop the EdmMetadata
table since it is no longer needed.
Once you do these things, you should be good to go with Migrations. New developers can run Update-Database
to create the database from scratch, and you can run Update-Database
(or use the Migrations initializer) against production to apply additional migrations there.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With