Unable to run EF5 migration with existing database

我的未来我决定 提交于 2019-11-29 06:56:07

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.

I would suggest a slightly different approach that will leave you in a state where you can use migrations to create databases from scratch in your development environment:

  1. Rather than calling add-migration -IgnoreChanges InitialSchema, try to get the generation of the initial schema migration to work. As you say, this should happen when you initially called Enable-Migrations. You could try pointing your database connection at a non-existent database to get this to work.

  2. As a result of this, your InitialSchema migration will contain the logic to create the database as it was at the point when you reverse-engineered it. You need to comment out the contents of the Up() and Down() until you've deployed to all the environments that already have an existing database.

  3. Then you can uncomment the contents of those methods and from then on, in your development environments you can drop the databases and recreate them using

    var migrator = new DbMigrator(new Configuration());
    migrator.Update();
    

    and you'll have the full set of migrations in your __MigrationHistory table. This important because without it you won't be able to add new migrations in the future.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!