Entity Framework Code Migrations - Stuck on Initial Migration

自古美人都是妖i 提交于 2019-11-28 20:35:30
MrUpsideDown

I had the same problem enabling EF migrations for a code-first model with an existing database, and the following procedure worked:

  1. Remove the existing Migrations folder in your project, and DROP the table __MigrationHistory from the existing database.
  2. Run the enable-migrations command from the Package Manager Console.
  3. Run the add-migration command to create an initial migration.
  4. Remove all of the code in Up() method for the initial migration.
  5. Run the 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.
  6. Make changes to your code-first model.
  7. Run the 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.
  8. Run the 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).

Did you try using the -force parameter to apply the changes.

Update-Database [-SourceMigration <String>]
  [-TargetMigration <String>] [-Script] [-Force] [-ProjectName <String>]
  [-StartUpProjectName <String>] [-ConfigurationTypeName <String>]
  [-ConnectionStringName <String>] [<CommonParameters>]

-FORCE Specifies that data loss is acceptable during automatic migration of the database.

You can use get-help Update-Database -examples to see usage examples.

Further read: EF Code First Migrations

This is a blanket approach that usually works:

  1. Delete your entire Migrations folder (make sure that you copy any code that you might have created from the seed method in your migration configuration file).
  2. Delete the actual database. If you are using LocalDb, this will normally sit within your AppData solution folder (Right-click -> open folder location). Make sure to delete the .mdf & .log database files.
  3. Go to Package Manager Console. Enter enable-migrations -projectname yourprojectname
  4. Go to Package Manager Console. Enter add-migration "Initial" -projectname yourprojectname.
  5. Open the migration configuration file and paste the code that you copied from step 1 into the seed method.
  6. Go to Package Manager Console. Enter update-database -projectname yourprojectname

This should do the trick.

Trying to migrate an old db version to a new model, either the database didn't match the new model or i got errors like :

Type is not resolved for member 'Npgsql.PostgresException,Npgsql, Version=3.2.2.0, Culture=neutral, PublicKeyToken=5d8b90d52f46fda7'

Here's how it worked (using automatic migration) :

  1. Delete Migrations folder
  2. Execute enable-migrations
  3. Set this two properties to true in the newly created Configuration.cs

    public Configuration()
    {
        AutomaticMigrationsEnabled = true;
        AutomaticMigrationDataLossAllowed = true;
    }
    
  4. Execute Update-Database -Force

Your database will be updated to latest scheme and ready.

hope this helps.

You do not need to remove the migration manually, remove it conveniently with

Remove-Migration

then you can recreate the migration script again with Add-Migration.

In your case, updating the database failed because there are existing tables, drop them with

Drop-Database

Then you can Update-Database.

More detailed usage of these commands are here.

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