我在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表的导航属性),并且他们迁移得很好。
有什么想法吗?
答案 0 :(得分:1)
问题是我在ChargeError表中的数据在ChargeTransactionId中为0,并且ChargeTransaction中没有行,因此ChargeTransaction中没有行,Id = 0。
删除ChargeError中的所有行后,更新正常进行。