Error while running Update-Database

asp.net-mvc ef-migrations entity-framework

Question

Running into the following errorUpdate-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, 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, 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.InternalDispatcher1.Dispatch[TInterceptionContext,TResult](Func1 operation, TInterceptionContext interceptionContext, Action1 executing, Action1 executed) at System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.NonQuery(DbCommand command, DbCommandInterceptionContext interceptionContext) at System.Data.Entity.Internal.InterceptableDbCommand.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.ExecuteStatementsInternal(IEnumerable
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](Func1 operation) at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute(Action operation) 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, IEnumerable1 systemOperations, Boolean downgrading, Boolean auto)
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
1 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.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.

Before doing this, I changed theID Model based on Code classes variables fromInt16 to Int32 .

Can you please help?

1
5
2/4/2014 5:37:32 AM

Accepted Answer

Because another table includes a foreign key referencing the ID column, it appears that the database cannot change or remove the ID column. You should be set to go if you remove the reference, update the column, and then add the reference again.

8
2/3/2014 8:56:01 PM

Popular Answer

PROBLEM SOLVED: After deleting all migration related folders, files and tables, I re enable the Migrations and repopulated the database as you've said

Here are explicit procedures to resolve the issue without deleting any migrations or manually altering one, building on the acceptable response.

In my circumstance, I had

public class Parent
{
    public virtual ICollection<Child> A { get; set; }
}

It was necessary to change the child's type from Child to OtherChild. I ran into the problem at that point.

I made two migrations to get around it. The first just unmapped EF's property A. I could have left it out by commenting it out, but a lot of the code that accesses the property would not have compiled.

public class Parent
{
    [NotMapped]
    public virtual ICollection<Child> A { get; set; }
}

add-migration UnmapA

update-database

then I modified the type and remapped the property.

public class Parent
{
    public virtual ICollection<OtherChild> A { get; set; }
}

add-migration ChangeChildType

update-database



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow