First, I've read these questions/answers:
- EF-migration message
- How can I stop Add-Migration checking my database has no pending migrations when using Code-Based migrations?
- Make EF4.3 Code First Migrations ignore pending migrations
These all seem to be for EF versions prior to EF5, and my situation doesn't seem to fit these answers. So, let me describe my situation.
- My application was originally created using EF4, model first. I designed my database with the GUI designer, and used it to generate my database.
- I've been running and collecting data into the database for a few months. I really can't lose this data.
- I branched my code, installed EF5 with NuGet, and used EF Power Tools to generate my model from my database by right-clicking a new class library project, and selecting
Entity Framework | Reverse engineer code first
. - I was able to smoothly re-reference to my new project, convert my project to use the new DbContext instead of ObjectContext, and removed the EF4 class library that held my old model. The program works great!
Now, I want to try out automatic migrations, which I've had a tiny bit of experience with in Ruby on Rails. Here's what I did:
- Ran
Enable-Migrations
. Had a bit of trouble due to connection strings and which app.config was getting used, but eventually got it. However, this MSDN page says that this should have automatically generated the first migration to get me to the point I'm already at. It didn't. - Ran
Add-Migration InitialSchema
to accomplish what wasn't automatically done in step 1. This worked. - Added a property to one of my model objects, then attempted to run
Add-Migration AddSerialToLogEntries
, and was presented with:
Unable to generate an explicit migration because the following explicit migrations are pending: [201307190100268_InitialSchema]. Apply the pending explicit migrations before attempting to generate a new explicit migration.
Attempting to apply the migration on my existing database failed, which isn't surprising.
The other answers I mentioned above basically said that I'm out of luck, but like I said those were for older versions of the Entity Framework. Do I have any options here?
While writing this question, I thought I might be able to use SQL Server Management Studio to export my data to a SQL script, delete the entire database, let EF create it, then run the scripts to get my data back in... I will try that tomorrow when I have time, but I would like to hear if there are other options as I'm not 100% sure that would work and would hate to have any errors in the data inserted in the process.