I have added EF 5 to my project via Nuget and enabled migrations with the "Enable-Migrations" command. I have then called "Add-Migration" to generate the base code for generating the schema.
I then added a property to one of my domain objects (string property called "TestProperty") and added a mapping to my EntityTypeConfiguration file (we're ignoring the conventions at the moment).
Calling "Add-Migration" again produces the error:
Unable to generate an explicit migration because the following explicit migrations are pending: [201303262144218_Initial]. Apply the pending explicit migrations before attempting to generate a new explicit migration.
But calling "Update-Database" produces a sql exception because the tables already exist:
There is already an object named 'Customer' in the database
In my constructor for my DbContext I have tried the different update strategies, e.g.:
Database.SetInitializer<UnitOfWork>(new DropCreateDatabaseAlways<UnitOfWork>());
Am I missing something obvious? I tried the solution here but it didn't work: Automatic Migrations for ASP.NET
Thanks
EDIT: Update The key to getting past the first step is to create the initial migration and then delete the generated code from the Up and Down methods (http://thedatafarm.com/blog/data-access/using-ef-migrations-with-an-existing-database/).
I can then update the model and the EF map and then run Add-Migration. This generates a migration with the correct Up and Down code.
The problem is then trying to apply the update. Update-Database produces the error "Unable to update database to match the current model because there are pending changes and automatic migration is disabled...automatic migration. Set DbMigrationsConfiguration.AutomaticMigrationsEnabled to true to enable automatic migration. You can use the Add-Migration command to write the pending model changes to a code-based migration". Ok, so I try Add-Migration again and it produces another migration with the exact same code as the last one.
I run Update-Database and get the same error again. I try "Update-Database -TargetMigration 201304080859556_MyMigration -Force" but this produces "The specified target migration '201304080859556_MyMigration' does not exist. Ensure that target migration refers to an existing migration id" - It does!
Very frustrating!
Run the Add-Migration InitialCreate –IgnoreChanges command in Package Manager Console. This creates an empty migration with the current model as a snapshot. Run the Update-Database command in Package Manager Console. This will apply the InitialCreate migration to the database.
Delete your Migrations folder. Create a new migration and generate a SQL script for it. In your database, delete all rows from the migrations history table. Insert a single row into the migrations history, to record that the first migration has already been applied, since your tables are already there.
I had the same problem enabling EF migrations for a code-first model with an existing database, and the following procedure worked:
__MigrationHistory
from the existing database.enable-migrations
command from the Package Manager Console.add-migration
command to create an initial migration.Up()
method for the initial migration.update-database
command to apply the initial migration to your database. This doesn't make any changes to existing objects (because the Up()
method contains no code), but it marks the existing database as having been migrated to the initial state.add-migration
command to create a new migration. The code in the Up()
method of the new migration will contain only the changes to your object model.update-database
command to apply the changes to your database.I run Update-Database and get the same error again. I try "Update-Database -TargetMigration 201304080859556_MyMigration -Force" but this produces "The specified target migration '201304080859556_MyMigration' does not exist. Ensure that target migration refers to an existing migration id" - It does!
There is one more issue which may cause your last error (and maybe it's a root cause of previous ones). I had a similar problem and it turned out that for some strange reason some of my migration classes where in a different namespace than the namespace of my MigrationConfiguration
class. Correcting namespaces (also in xxx.Designer.cs
files) solved this issue (migrations were visible and working again).
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