Doug
Doug

Reputation: 15515

Entity Framework foreign key code first migration error

I've got an entity framework class with two a reference back to another object

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

}

This works fine when I generate a migration using Add-Migration

Now, I added a second entry

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

When I run the migration, it does some weird stuff, like renaming the original column.

   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");

That would be fine, but the migration fails when i run 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.

Not sure what to do here. Any suggestions?

Upvotes: 3

Views: 5378

Answers (1)

Z .
Z .

Reputation: 12837

since you are not following the convention (Feature_ID) you need to help it a bit: on the FeatureId property put an attribute [ForeignKey("Feature")]

[ForeignKey("Feature")]
public int? FeatureId { get; set; }

public Feature Feature { get; set; }

Upvotes: 4

Related Questions