![](/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.