0

我在 VS2010 中将 EF5 与 Code First 与 MVC4 一起使用。

对于模型,我有一个 ChargeTransaction 模型和一个 ChargeError 模型。ChargeTransactions -> ChargeError 是 1-M 关系。

这是模型类

[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;}
    }

当我将 ChargeTransaction 导航属性添加到 ChargeError 并添加迁移然后调用 update-database 时,我收到以下错误:

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'.

我在其他表之间有导航属性(例如,在 ChargeTransaction 中,我有一个 Account 表的导航属性)并且它们迁移得很好。

有任何想法吗?

4

1 回答 1

1

问题是我在 ChargeError 表中有数据,ChargeTransactionId 中的值为 0,而 ChargeTransaction 中没有行,因此 ChargeTransaction 中没有 Id=0 的行。

一旦我删除了 ChargeError 中的所有行,更新就会正常进行。

于 2013-10-29T16:22:58.457 回答