使用Entity Framework 5和nuget重新创建表 [英] Re-create table with Entity Framework 5 and nuget

查看:134
本文介绍了使用Entity Framework 5和nuget重新创建表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Code First创建一个表。

I am using Code First to create a table.

我创建了类,映射文件,并在nuget中发出了add-migration命令,然后发出了update-database命令

I created the class, the mapping file and issued the add-migration command in nuget and then the update-database command

然后我更改了类,就像白痴一样删除了表。

I then changed the class and like an idiot deleted the table.

我删除了迁移类文件

我发出了添加迁移命令

当我发出update-database命令时,出现以下错误:

When I issue the update-database command I get the following error:


System.Data.SqlClient.SqlException(0x80131904):找不到
对象 dbo.CorrectiveActionPlan,因为它不存在或您确实
没有权限。
处System.Data.SqlClient.SqlInternalConnection.OnError(SqlException异常,
布尔值BreakConnection,Action 1 wrapCloseInAction) SqlException
异常,布尔值breakConnection,在
处的动作
1 wrapCloseInAction)System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject
stateObj,布尔值调用程序HasConnectionLock,布尔值asyncClose)
System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior,
SqlCommand cmdHandler,SqlDataReader dataStream,
BulkCopySimpleResultSet bulkCopyHandler,TdsParserStateObject
stateObj,Boolean& dataReady)在
System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String
methodName,Boolean async,Int32 timeout)at
System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource 1
完成,字符串methodName,布尔型sendToPipe,Int32超时,
布尔值asyncWrite)在
处System.Data.SqlClient.SqlCommand.ExecuteNonQuery()在
处System.Data.Entity.Migrations.DbMigrator.ExecuteSql(DbTransaction
事务,MigrationStatement migrationStatement)在
System.Data.Entity.Migrations.DbMigrator.ExecuteStatements(IEnumerable 处的$ b System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.ExecuteSql(DbTransaction
transaction,MigrationStatement migrationStatement)在
处> 1
migrationStatements)在System.Data处
System.Data.Entity.Migrations.Infrastructure.MigratorBase.ExecuteStatements(IEnumerable 1
migrationStatements)。 Entity.Migrations.DbMigrator.ExecuteOperations(String
migrationId,XDocument targetModel,IEnumerable
1操作,布尔
降级,布尔自动)在
System.Data.Entity处。在
处的Migrations.DbMigrator.ApplyMigration(DbMigration
迁移,DbMigration lastMigration)System.Data.Enti ty.Migrations.Infrastructure.MigratorLoggingDecorator.ApplyMigration(DbMigration
迁移,DbMigration lastMigration)位于
System.Data.Entity.Migrations.DbMigrator.Upgrade(IEnumerable 1
待定迁移,字符串targetMigrationId,字符串lastMigrationId)

处的
System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.Upgrade(IEnumerable
1
endingMigrations,字符串targetMigrationId ,位于System.Data.Entity.Migrations.DbMigrator.Update处的字符串lastMigrationId)

(位于
处的字符串
targetMigration),位于System.Data.Entity.Migrations.Infrastructure.MigratorBase.Update(
处的字符串
targetMigration)System.Data.Entity.Migrations.Design.ToolingFacade.UpdateRunner.RunCore()
在System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.Run( )
ClientConnectionId:a6e92a35-cc9e-4867-97a5-0a274081d853找不到
对象 dbo.CorrectiveActionPlan,因为它不存在或您
没有权限。

System.Data.SqlClient.SqlException (0x80131904): Cannot find the object "dbo.CorrectiveActionPlan" because it does not exist or you do not have permissions. at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action1 wrapCloseInAction) at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action1 wrapCloseInAction) at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady) at System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean async, Int32 timeout) at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean asyncWrite) at System.Data.SqlClient.SqlCommand.ExecuteNonQuery() at System.Data.Entity.Migrations.DbMigrator.ExecuteSql(DbTransaction transaction, MigrationStatement migrationStatement) at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.ExecuteSql(DbTransaction transaction, MigrationStatement migrationStatement) at System.Data.Entity.Migrations.DbMigrator.ExecuteStatements(IEnumerable1 migrationStatements) at System.Data.Entity.Migrations.Infrastructure.MigratorBase.ExecuteStatements(IEnumerable1 migrationStatements) at System.Data.Entity.Migrations.DbMigrator.ExecuteOperations(String migrationId, XDocument targetModel, IEnumerable1 operations, Boolean downgrading, Boolean auto) at System.Data.Entity.Migrations.DbMigrator.ApplyMigration(DbMigration migration, DbMigration lastMigration) at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.ApplyMigration(DbMigration migration, DbMigration lastMigration) at System.Data.Entity.Migrations.DbMigrator.Upgrade(IEnumerable1 pendingMigrations, String targetMigrationId, String lastMigrationId)
at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.Upgrade(IEnumerable
1 pendingMigrations, String targetMigrationId, String lastMigrationId)
at System.Data.Entity.Migrations.DbMigrator.Update(String targetMigration) at System.Data.Entity.Migrations.Infrastructure.MigratorBase.Update(String targetMigration) at System.Data.Entity.Migrations.Design.ToolingFacade.UpdateRunner.RunCore() at System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.Run() ClientConnectionId:a6e92a35-cc9e-4867-97a5-0a274081d853 Cannot find the object "dbo.CorrectiveActionPlan" because it does not exist or you do not have permissions.

如何强制EF重新创建表?

How do I force EF to recreate the table?

推荐答案

我找到了答案。

我删除了[dbo]中的行。[__ MigrationHistory]与我的迁移

I deleted the row in [dbo].[__MigrationHistory] that corresponded to my Migration

然后我删除了新的迁移文件

I then deleted the new migration file

我重新运行了添加迁移

,然后重新运行update-database -verbose

and then re-ran update-database -verbose

这篇关于使用Entity Framework 5和nuget重新创建表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆