如何重新创建我的EF CodeFirst表?

Pin*_*nch 9 entity-framework code-first

我创建了一个Code First类

数据库是在Sql中自动创建的.

我删除了表:(

我启用了迁移:

AutomaticMigrationsEnabled = true;
AutomaticMigrationDataLossAllowed = true;
Database.SetInitializer(new DropCreateDatabaseAlways<SurveyContext>());
Run Code Online (Sandbox Code Playgroud)

然而

update-database -force

Cannot find the object "dbo.CustomerResponses" because it does not exist or you do not have permissions.
Run Code Online (Sandbox Code Playgroud)

请帮忙.

Pin*_*nch 9

在系统表中有一个名为的表

__MigrationHistory
Run Code Online (Sandbox Code Playgroud)

...我已经把它删了.

在包管理器控制台中运行命令"update-database"问题已修复!

更多信息:

http://blog.oneunicorn.com/2012/02/27/code-first-migrations-making-__migrationhistory-not-a-system-table/


jk7*_*jk7 5

Daf De Giraf在这里有一个更好的答案,它不会消除您的迁移历史。

如果您使用该命令以正确的方式创建迁移,Add-Migration "Name_Of_Migration"那么您可以执行以下操作以获得干净的开始(重置,当然会丢失数据):

  1. Update-database -TargetMigration:0
    通常你的数据库现在是空的,因为执行了 down 方法。

  2. Update-database 这将重新创建您的数据库到您当前的迁移


Rui*_*mba 0

我以前也有过这样的错误信息......

检查是否创建了您在连接字符串中指定的数据库。如果不是,请创建它,然后执行代码。