I upgraded a project to Entity Framework 4.3 and enabled migrations on the project.
However, I get this error when running the Update-Database command:
Cannot scaffold the next migration because the target database was created with a version of Code First earlier than EF 4.3 and does not contain the migrations history table. To start using migrations against this database, ensure the current model is compatible with the target database and execute the migrations Update process. (In Visual Studio you can use the Update-Database command from Package Manager Console to execute the migrations Update process).
Basically, it is telling me to run the same command (Update-Database) that is giving me the error.
Any ideas?
Not exactly a "fun" way to do it, but I let the app create a new database, which creates a system table called "__MigrationHistory". I then ran the following script to create that table on my old database. I also created a script to copy the one row that existed in the new database to the old database.
If someone from Microsoft or community knows a more efficient way of doing this, please post here!
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[__MigrationHistory](
[MigrationId] [nvarchar](255) NOT NULL,
[CreatedOn] [datetime] NOT NULL,
[Model] [varbinary](max) NOT NULL,
[ProductVersion] [nvarchar](32) NOT NULL,
CONSTRAINT [PK___MigrationHistory] PRIMARY KEY CLUSTERED
(
[MigrationId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
When you ran Enable-Migrations
, the scripts may have not created an initial migration, especially if your model was mismatched to the database, or if your DbContext
class is defined in a different project.
I'm not sure what the "right way" to add migrations to an existing pre-4.3 database is, but the easiest way is to dump your database. Drop the tables in it...
Make sure the Configuration file in the Migrations folder has the right context type, and then Add-Migration Initial
. A big class will get built, representing your current model.
Now Update-Database
will run without complaining.
If you have data that you care about in the db, you may be able to cheat and add the __MigrationHistory
table that the migrations process creates to a previous backup of your database. Delete the EdmMetadata
and migrations shouldn't be any wiser.
Hopefully someone who actually knows how migrations were intended to be added to an existing EF Code First database will have smoother upgrade steps?
I just ran into this on a database I "thought" was already 4.3 but was not....
I found the answer with this blog post.
Here's the basic steps.
- Don't make any changes to the model, really, don't!
- Add an initial migration.
- Update the database (all of them).
Now you can go about your normal business.
Here's the nitty gritty:
Don't change the model.
If you have, you need to back those out. Ugggg. I just commented out my changes. Luckily, for me, the changes were still fairly small. Of course you are doing things in small chunks anyway right. :-)
Add an initial migration.
Add-Migration "InitialModel" -IgnoreChanges
In the up script add the following:
public override void Up()
{
Sql("DROP TABLE EdmMetadata");
}
Adding the drop table is not necessary but 4.3 and on don't use EdmMetadata so, might as well.
Update the Database (all of them)
Update-Database
Do you have other servers for this database? Testing, Staging, Production? Be sure to do this final step for all of them as well. You can wait until you are done with all of your migration work before doing this to the other servers as well.
Now, continue on as normal. Make your changes and follow the normal Add-Migration
& Update-Database
steps for migrations.
Thanks for the question and answers. I've done the following (a composition of advises above).
How to migrate from pre-EF 4.3 with data and Code First model:
- Comment out all changes done comparing to current data (I needed to remove table adding).
- Backup database
- Remove table
EdmMetadata
- Add table
__MigrationHistory
with the script above
- Run
Add-Migration "InitialModel"
- Drop all tables except the
__MigrationHistory
table
- Run
Update-database
- Generate script for
__MigrationHistory
with the newly added data. Add there Drop table EdmMetadata
.
- Restore database and run this script.
- Get back the changes in the code.
- Run
Add-Migration YOURNAMEFORNEWCHANGES
- Run
Update-Database
And I have the old database with the data updated to EF 6. Hope that helps!