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 yourDbContextclass 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-Databasewill run without complaining.If you have data that you care about in the db, you may be able to cheat and add the
__MigrationHistorytable that the migrations process creates to a previous backup of your database. Delete theEdmMetadataand 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?