Asp.net mvc 4 添加导航属性时EF 5迁移更新数据库失败
我在VS2010中首先使用带有代码的EF5和MVC4 对于模型,我有一个ChargeTransaction模型和一个ChargeError模型。ChargeTransactions->ChargeError是一个1-M关系 下面是模型类Asp.net mvc 4 添加导航属性时EF 5迁移更新数据库失败,asp.net-mvc-4,entity-framework-5,entity-framework-migrations,Asp.net Mvc 4,Entity Framework 5,Entity Framework Migrations,我在VS2010中首先使用带有代码的EF5和MVC4 对于模型,我有一个ChargeTransaction模型和一个ChargeError模型。ChargeTransactions->ChargeError是一个1-M关系 下面是模型类 [Table("ChargeTransaction")] public class ChargeTransaction { public int Id { get; set; } public int Accoun
[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并添加迁移,然后调用更新数据库时,出现以下错误:
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表具有导航属性),并且它们迁移良好
有什么想法吗?问题是我在ChargeError表中的数据在ChargeTransactionId中有0,并且ChargeTransaction中没有行,因此ChargeTransaction中没有Id为0的行 一旦我删除了ChargeError中的所有行,更新将正常进行