Rick Hodder
Rick Hodder

Reputation: 2252

EF 5 Migration Update-Database Failing When Navigation Property Added

I am using EF5 with Code First with MVC4 in VS2010.

For models, I have a ChargeTransaction model and a ChargeError model. ChargeTransactions -> ChargeError is a 1-M relationship.

Here are the model classes

[Table("ChargeTransaction")]
    public class ChargeTransaction
    {
        public int Id { get; set; }
        public int AccountId { get; set; }
        public DateTime Created { get; set; }

        public virtual Account Account { get; set; }
    }

    [Table("ChargeError")]
    public class ChargeError
    {        
        public int Id { get; set; }
        public int ChargeTransactionId { get; set; }

        // adding this line breaks update-database
        public virtual ChargeTransaction ChargeTransaction {get;set;}
    }

When I add the ChargeTransaction navigation property to ChargeError and add a migration and then call update-database, I get the following error:

Target database is: 'ChargesInterfaceDashboard' (DataSource: localhost, Provider: System.Data.SqlClient, Origin: Configuration).
Applying code-based migrations: [201310282228417_stuff 2].
Applying code-based migration: 201310282228417_stuff 2.
ALTER TABLE [dbo].[ChargeError] ADD CONSTRAINT [FK_dbo.ChargeError_dbo.ChargeTransaction_ChargeTransactionId] FOREIGN KEY ([ChargeTransactionId]) REFERENCES [dbo].[ChargeTransaction] ([Id]) ON DELETE CASCADE
System.Data.SqlClient.SqlException (0x80131904): The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "FK_dbo.ChargeError_dbo.ChargeTransaction_ChargeTransactionId". The conflict occurred in database "ChargesInterfaceDashboard", table "dbo.ChargeTransaction", column 'Id'.
   at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)
   at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning()
   at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
   at System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean async)
   at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe)
   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.ExecuteOperations(String migrationId, XDocument targetModel, IEnumerable`1 operations, Boolean downgrading, Boolean auto)
   at System.Data.Entity.Migrations.DbMigrator.ApplyMigration(DbMigration migration, DbMigration lastMigration)
   at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.ApplyMigration(DbMigration migration, DbMigration lastMigration)
   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.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()
The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "FK_dbo.ChargeError_dbo.ChargeTransaction_ChargeTransactionId". The conflict occurred in database "ChargesInterfaceDashboard", table "dbo.ChargeTransaction", column 'Id'.

I have navigation properties between other tables (for example in ChargeTransaction I have a navigation property for the Account table) and they migrated fine.

Any ideas?

Upvotes: 0

Views: 833

Answers (1)

Rick Hodder
Rick Hodder

Reputation: 2252

The issue was that I had data in the ChargeError table that had 0 in the ChargeTransactionId, and there are no rows in ChargeTransaction, so there are no rows in ChargeTransaction with an Id=0.

Once I deleted all rows in ChargeError, the update proceeded as normal.

Upvotes: 1

Related Questions