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 g
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
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.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.
1) Remove the existing Migrations folder in your project, and delete existing migration from Migration_History table if any.
2)Run the following command from the package manager console:
add-migration reset
3)After that run the following command from the package manager console:
Remove-Migration
4)After that run the following command from the package manager console (InitialMigration is the name of the first migration, you can name as you want ):
add-migration InitialMigration
This is a blanket approach that usually works:
enable-migrations -projectname yourprojectname
add-migration "Initial" -projectname yourprojectname
.update-database -projectname yourprojectname
This should do the trick.