Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
246 views
in Technique[技术] by (71.8m points)

c# - Unable to run EF5 migration with existing database

First, I've read these questions/answers:

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.

  1. My application was originally created using EF4, model first. I designed my database with the GUI designer, and used it to generate my database.
  2. I've been running and collecting data into the database for a few months. I really can't lose this data.
  3. 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.
  4. 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:

  1. 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.
  2. Ran Add-Migration InitialSchema to accomplish what wasn't automatically done in step 1. This worked.
  3. 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

In fact the enable-migrations command creates an initial migration only if your database has already been created with Code-First before in which case the database contains a __MigrationHistory table. If this table does not exist (which is the case when you have an existing database that never has been created before with Code-First) enable-migrations only creates the Configuration class. You have to call add-migration manually then to create the first migration class. So, the behaviour you have seen is expected.

Generally the procedure to prepare an existing database for migrations is the following if you are using EF 5:

  • Call enable-migrations in package manager console. A Migrations folder in your project and a Configuration class will be created.

  • Open the Configuration class and set AutomaticMigrationsEnabled = false in the constructor (if it isn't already by default).

  • In package manager console call

    add-migration -IgnoreChanges InitialSchema
    

    "InitialSchema" is only an example name. You can name it like you want. A <Timestamp>_InitialSchema class will be created that derives from DbMigration. The Up and Down methods in this class are empty due to the -IgnoreChanges flag. Without this flag the class would contain a migration to add your whole model to the database which is not what you want since the existing database already contains the database schema.

  • Run update-database in the package manager console. Because the Up method is empty this update does nothing with your existing schema except it creates the __MigrationHistory table (as a system table in your database) and adds the first record to this table that contains a model hash of your current EF model.

  • Optional last step: If you prefer to work with automatic migrations open the Configuration class and set AutomaticMigrationsEnabled = true in the constructor. If you want to proceed with code-based migrations leave the flag false.

At this point you can start to make changes to your model. Everytime you create a new migration with add-migration it will be based on your model before the modification and the migration class will only contain the necessary schema changes.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...