我有一个实体框架类,其中有两个引用回另一个对象
public class Review
{
[Key]
public int Id
{get;set;}
public int? FeatureId {get;set;}
public Feature Feature {get;set;}
}
public class Feature
{
[Key]
public int Id
{get;set;}
public ICollection<Review> Reviews {get;set;}
}
当我使用生成迁移时,这工作正常Add-Migration
现在,我添加了第二个条目
public class Review
{
[Key]
public int Id
{get;set;}
public int? FeatureId {get;set;}
public Feature Feature {get;set;}
public int? CompareToFeatureId {get;set;}
public Feature CompareToFeature {get;set;}
}
当我运行迁移时,它会做一些奇怪的事情,比如重命名原始列。
RenameColumn(table: "dbo.Reviews", name: "FeatureId", newName: "Feature_Id");
AddColumn("dbo.Reviews", "CompareToFeatureId", c => c.Int());
AddForeignKey("dbo.Reviews", "FeatureId", "dbo.Features", "Id");
AddForeignKey("dbo.Reviews", "CompareToFeatureId", "dbo.Features", "Id");
CreateIndex("dbo.Reviews", "FeatureId");
CreateIndex("dbo.Reviews", "CompareToFeatureId");
那很好,但是当我运行时迁移失败update-database
Applying code-based migrations: [201211092218271_Test2].
Applying code-based migration: 201211092218271_Test2.
System.Data.SqlClient.SqlException (0x80131904): Foreign key 'FK_dbo.Reviews_dbo.Features_FeatureId' references invalid column 'FeatureId' in referencing table 'Reviews'.
Could not create constraint. See previous errors.
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 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)
at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean asyncWrite)
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()
ClientConnectionId:f1573dba-4658-462f-969a-9afbf1e4374a
Foreign key 'FK_dbo.Reviews_dbo.Features_FeatureId' references invalid column 'FeatureId' in referencing table 'Reviews'.
Could not create constraint. See previous errors.
不知道在这里做什么。有什么建议么?