实体框架 – 更新到EF 6.0.0-alpha后,无法使用EF迁移更新数据库

前端之家收集整理的这篇文章主要介绍了实体框架 – 更新到EF 6.0.0-alpha后,无法使用EF迁移更新数据库前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
将我的实体框架更新为6.0.0-alpha1后,我无法更新数据库.
这是我得到的错误
PM> update-database -verbose
Using StartUp project 'DataCenter'.
Using NuGet project 'DataCenter.Domain'.
Specify the '-Verbose' flag to view the sql statements being applied to the target database.
Target database is: 'datacenter' (DataSource: .,Provider: System.Data.sqlClient,Origin: Configuration).
Upgrading history table.
ALTER TABLE [dbo].[__MigrationHistory] ADD [ContextKey] [nvarchar](512) NOT NULL DEFAULT 'DataCenter.Domain.Migrations.Configuration'
ALTER TABLE [dbo].[__MigrationHistory] DROP CONSTRAINT [PK_dbo.__MigrationHistory]
System.Data.sqlClient.sqlException (0x80131904): 'PK_dbo.__MigrationHistory' is not a constraint.
Could not drop constraint. See prevIoUs errors.
   at System.Data.sqlClient.sqlConnection.OnError(sqlException exception,Boolean breakConnection,Action`1 wrapCloseInAction)
   at System.Data.sqlClient.sqlInternalConnection.OnError(sqlException exception,Action`1 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(TaskCompletionSource`1 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(IEnumerable`1 migrationStatements)
   at System.Data.Entity.Migrations.Infrastructure.MigratorBase.ExecuteStatements(IEnumerable`1 migrationStatements)
   at System.Data.Entity.Migrations.DbMigrator.UpgradeHistory(IEnumerable`1 upgradeOperations)
   at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.UpgradeHistory(IEnumerable`1 upgradeOperations)
   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:eda1a366-d3c8-44de-8cc9-8f64e2511b4e
'PK_dbo.__MigrationHistory' is not a constraint.
Could not drop constraint. See prevIoUs errors.

这是什么原因?我查询了“PK_dbo .__ MigrationHistory”的sysobjects表,但不存在.
任何想法?

提前致谢.

解决方法

好.我找到了解决方案.
看来,EF希望__MigrationHistory表的主键约束具有名称PK_dbo .__MigrationHistory.但它是PK___MigrationHistory.

所以下面的sql语句将会做到这一点.

ALTER TABLE __MigrationHistory
drop CONSTRAINT PK___MigrationHistory
ALTER TABLE __MigrationHistory
ADD CONSTRAINT [PK_dbo.__MigrationHistory] PRIMARY KEY (MigrationId)

之后我已经使用update-database没有错误.

猜你在找的MsSQL相关文章