![](/img/trans.png)
[英]update-database fires: “There is already an object named 'Adresses' in the database”.
[英]Update-Database MVC automatic migrations - There is already an object named 'StationsArrangeds' in the database
Update-Database
命令給出了這個錯誤:
數據庫中已經有一個名為“StationsArrangeds”的對象。
所以我刪除了這個表,現在我在嘗試更新數據庫時遇到了這個異常。
使用啟動項目“MetroTrain”。 使用 NuGet 項目“MetroTrain”。 指定“-Verbose”標志以查看應用於目標數據庫的 SQL 語句。
System.Data.Entity.Core.EntityCommandExecutionException:執行命令定義時出錯。 有關詳細信息,請參閱內部異常。
System.Data.SqlClient.SqlException: 無效的對象名稱“dbo.StationsArranged”。
在 System.Data.SqlClient.SqlConnection.OnError(SqlException 異常,Boolean breakConnection,Action
1 wrapCloseInAction)
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action1 wrapCloseInAction)
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action1 wrapCloseInAction)
1 wrapCloseInAction)
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action
在 System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
在 System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
在 System.Data.SqlClient.SqlDataReader.TryConsumeMetaData()
在 System.Data.SqlClient.SqlDataReader.get_MetaData()
在 System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
在 System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, SqlDataReader ds, Boolean describeParameterEncryptionRequest)
在 System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String 方法, TaskCompletionSource1 completion, Int32 timeout, Task& task, Boolean asyncWrite)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method)
at System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
at System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior)
at System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.<>c__DisplayClassb.<Reader>b__8()
at System.Data.Entity.Infrastructure.Interception.InternalDispatcher1 completion, Int32 timeout, Task& task, Boolean asyncWrite)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method)
at System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
at System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior)
at System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.<>c__DisplayClassb.<Reader>b__8()
at System.Data.Entity.Infrastructure.Interception.InternalDispatcher1 completion, Int32 timeout, Task& task, Boolean asyncWrite)
1.Dispatch[TInterceptionContext,TResult](Func
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method)
at System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
at System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior)
at System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.<>c__DisplayClassb.<Reader>b__8()
at System.Data.Entity.Infrastructure.Interception.InternalDispatcher1 operation, TInterceptionContext interceptionContext, Action
1執行,Action1 executed)
at System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.Reader(DbCommand command, DbCommandInterceptionContext interceptionContext)
at System.Data.Entity.Internal.InterceptableDbCommand.ExecuteDbDataReader(CommandBehavior behavior)
at System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior)
at System.Data.Entity.Core.EntityClient.Internal.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior)
--- End of inner exception
Stack trace ---
at System.Data.Entity.Core.EntityClient.Internal.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior)
at System.Data.Entity.Core.Objects.Internal.ObjectQueryExecutionPlan.Execute[TResultType](ObjectContext context, ObjectParameterCollection parameterValues)
at System.Data.Entity.Core.Objects.ObjectQuery1 executed)
at System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.Reader(DbCommand command, DbCommandInterceptionContext interceptionContext)
at System.Data.Entity.Internal.InterceptableDbCommand.ExecuteDbDataReader(CommandBehavior behavior)
at System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior)
at System.Data.Entity.Core.EntityClient.Internal.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior)
--- End of inner exception
Stack trace ---
at System.Data.Entity.Core.EntityClient.Internal.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior)
at System.Data.Entity.Core.Objects.Internal.ObjectQueryExecutionPlan.Execute[TResultType](ObjectContext context, ObjectParameterCollection parameterValues)
at System.Data.Entity.Core.Objects.ObjectQuery1 executed)
1.<>c__DisplayClassb.b__a() 在 System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func
at System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.Reader(DbCommand command, DbCommandInterceptionContext interceptionContext)
at System.Data.Entity.Internal.InterceptableDbCommand.ExecuteDbDataReader(CommandBehavior behavior)
at System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior)
at System.Data.Entity.Core.EntityClient.Internal.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior)
--- End of inner exception
Stack trace ---
at System.Data.Entity.Core.EntityClient.Internal.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior)
at System.Data.Entity.Core.Objects.Internal.ObjectQueryExecutionPlan.Execute[TResultType](ObjectContext context, ObjectParameterCollection parameterValues)
at System.Data.Entity.Core.Objects.ObjectQuery1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess)
at System.Data.Entity.Core.Objects.ObjectQuery1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess)
at System.Data.Entity.Core.Objects.ObjectQuery1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess)
1.<>c__DisplayClassb.b__9()
at System.Data.Entity.Core.Objects.ObjectQuery
在 System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func1 operation)
at System.Data.Entity.Core.Objects.ObjectQuery1 operation)
at System.Data.Entity.Core.Objects.ObjectQuery1 operation)
1.GetResults(Nullable
at System.Data.Entity.Core.Objects.ObjectQuery1 forMergeOption)
at System.Data.Entity.Core.Objects.ObjectQuery1 forMergeOption)
at System.Data.Entity.Core.Objects.ObjectQuery1 forMergeOption)
1.System.Collections.Generic.IEnumerable.GetEnumerator>b__0()
at System.Data.Entity.Core.Objects.ObjectQuery
在 System.Lazy1.CreateValue()
at System.Lazy1.CreateValue()
at System.Lazy1.CreateValue()
1.LazyInitValue()
at System.Lazy
在 System.Lazy1.get_Value()
at System.Data.Entity.Internal.LazyEnumerator1.get_Value()
at System.Data.Entity.Internal.LazyEnumerator1.get_Value()
1.MoveNext()
at System.Data.Entity.Internal.LazyEnumerator
在 System.Collections.Generic.List1..ctor(IEnumerable
1 集合)
在 System.Linq.Enumerable.ToList[TSource](IEnumerable`1 源)
在 MetroTrain.Models.StationsContext..ctor() 在 c:\\Users\\ghousia pc\\Dropbox\\iMaq\\MetroTrain\\MetroTrain\\Models\\StationsContext.cs:line 22
--- 從上一個拋出異常的位置開始的堆棧跟蹤結束 >在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
在 System.Data.Entity.Infrastructure.DbContextInfo.CreateInstance()
在 System.Data.Entity.Infrastructure.DbContextInfo..ctor(Type contextType, DbProviderInfo modelProviderInfo, AppConfig config, DbConnectionInfo connectionInfo)
在 System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration 配置,DbContext usersContext)
在 System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration 配置)
在 System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.GetMigrator()
在 System.Data.Entity.Migrations.Design.ToolingFacade.UpdateRunner.Run()
在 System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
在 System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
在 System.Data.Entity.Migrations.Design.ToolingFacade.Run(BaseRunner runner)
在 System.Data.Entity.Migrations.Design.ToolingFacade.Update(String targetMigration, Boolean force)
在 System.Data.Entity.Migrations.UpdateDatabaseCommand.<>c__DisplayClass2.<.ctor>b__0()
在 System.Data.Entity.Migrations.MigrationsDomainCommand.Execute(Action command)執行命令定義時發生錯誤。 有關詳細信息,請參閱內部異常。
我已經通過論壇沒有找到解決方案。
首先手動刪除代碼中的表會導致 EF 無法跟蹤您的模型更改。
您可以通過刪除遷移歷史記錄表並重新啟用遷移來重置代碼優先遷移。
這主要是因為您的數據庫遷移表(“__MigrationHistory”)與您的遷移不同步,您可以在默認連接字符串中更改數據庫名稱並創建新數據庫。 創建新數據庫后,將其與現有數據庫菜單進行比較並將“__MigrationHistory”從新數據庫同步到現有數據庫以供以后遷移
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.