It looks like the database can't alter/drop the ID column because another table has a foreign key referencing the ID column. If you drop the reference, update the column, and then re-add the reference, you should be good to go...
Error while running Update-Database
-
06-10-2022 - |
Frage
I got the following error while running Update-Database -Verbose
:
Using StartUp project 'WebApplication'. Using NuGet project 'WebApplication'. Specify the '-Verbose' flag to view the SQL statements being applied to the target database. Target database is: 'Frappuccino' (DataSource: (Localdb)\v11.0, Provider: System.Data.SqlClient, Origin: Configuration). No pending explicit migrations. Applying automatic migration: 201402032027563_AutomaticMigration. ALTER TABLE [dbo].[Clients] ALTER COLUMN [ID] [int] NOT NULL System.Data.SqlClient.SqlException (0x80131904): The object 'PK_dbo.Clients' is dependent on column 'ID'. The object 'FK_dbo.Users_dbo.Clients_Client_ID' is dependent on column 'ID'. ALTER TABLE ALTER COLUMN ID failed because one or more objects access this column. at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action
1 wrapCloseInAction) at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, 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, Boolean asyncWrite) 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.Infrastructure.Interception.InternalDispatcher
1.Dispatch[TInterceptionContext,TResult](Func1 operation, TInterceptionContext interceptionContext, Action
1 executing, Action1 executed) at System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.NonQuery(DbCommand command, DbCommandInterceptionContext interceptionContext) at System.Data.Entity.Internal.InterceptableDbCommand.ExecuteNonQuery()
1 migrationStatements, DbConnection connection) at System.Data.Entity.Migrations.DbMigrator.<>c_DisplayClass32.b_2e() at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.<>c_DisplayClass1.b_0() at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func
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.ExecuteStatementsInternal(IEnumerable1 operation) at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute(Action operation) at System.Data.Entity.Migrations.DbMigrator.ExecuteStatements(IEnumerable
1 migrationStatements) at System.Data.Entity.Migrations.Infrastructure.MigratorBase.ExecuteStatements(IEnumerable1 migrationStatements) at System.Data.Entity.Migrations.DbMigrator.ExecuteOperations(String migrationId, XDocument targetModel, IEnumerable
1 operations, IEnumerable1 systemOperations, Boolean downgrading, Boolean auto)
1 pendingMigrations, String targetMigrationId, String lastMigrationId)
at System.Data.Entity.Migrations.DbMigrator.AutoMigrate(String migrationId, XDocument sourceModel, XDocument targetModel, Boolean downgrading) at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.AutoMigrate(String migrationId, XDocument sourceModel, XDocument targetModel, Boolean downgrading) at System.Data.Entity.Migrations.DbMigrator.Upgrade(IEnumerable
at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.Upgrade(IEnumerable`1 pendingMigrations, String targetMigrationId, String lastMigrationId)
at System.Data.Entity.Migrations.DbMigrator.UpdateInternal(String targetMigration) at System.Data.Entity.Migrations.DbMigrator.<>c_DisplayClassc.b_b() at System.Data.Entity.Migrations.DbMigrator.EnsureDatabaseExists(Action mustSucceedToKeepDatabase) at System.Data.Entity.Migrations.Infrastructure.MigratorBase.EnsureDatabaseExists(Action mustSucceedToKeepDatabase) 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.Run() at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate) at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
at System.Data.Entity.Migrations.Design.ToolingFacade.Run(BaseRunner runner) at System.Data.Entity.Migrations.Design.ToolingFacade.Update(String targetMigration, Boolean force) at System.Data.Entity.Migrations.UpdateDatabaseCommand.<>c_DisplayClass2.<.ctor>b_0() at System.Data.Entity.Migrations.MigrationsDomainCommand.Execute(Action command) ClientConnectionId:9854ba62-772c-4ba6-b185-ecae993e065f The object 'PK_dbo.Clients' is dependent on column 'ID'. The object 'FK_dbo.Users_dbo.Clients_Client_ID' is dependent on column 'ID'. ALTER TABLE ALTER COLUMN ID failed because one or more objects access this column.
What I did before this is to change the ID
variables from my Code-First Model classes from Int16
to Int32
.
Any help please?
Lösung
Andere Tipps
PROBLEM SOLVED: After deleting all migration related folders, files and tables, I re enable the Migrations and repopulated the database as you've said
Building on the accepted answer, here are specific steps to work around the issue without dropping all migrations or manually editing one.
In my situation I had
public class Parent
{
public virtual ICollection<Child> A { get; set; }
}
The type for the child had to be changed from Child to OtherChild. That is when I encountered the error.
To work around it, I created two migrations. The first simply unmapped the property A from EF. I could have also commented it out, but quite a bit of code accessing the property would have failed to compile.
public class Parent
{
[NotMapped]
public virtual ICollection<Child> A { get; set; }
}
add-migration UnmapA
update-database
then I re-mapped the property and changed the type
public class Parent
{
public virtual ICollection<OtherChild> A { get; set; }
}
add-migration ChangeChildType
update-database