3

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?

1 Answer 1

4

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; }
Sign up to request clarification or add additional context in comments.

4 Comments

This was part of it. The other part was that I had an ICollection<Feature> on the other side. When I added the second reference, it confused it, so I had to add an InverseProperty to the ICollection. Between the 2 of those fixes, it solved it.
As mentioned above, your naming convention is wrong. Both entities should have the same property of "FeatureId". It is not good naming convention to have just "Id" in your feature entity. Firstly entity framework looks for [entity name]Id property in your entity and will automatically map that as your primary key. Then simply use the same name in other entities along with "public virtual ICollection<Feature> Features {get;set;}" and EF will do the rest.
Code First also uses the convention of Id without [entity name] prefix, so there's nothing wrong with that. Using "Id" as a key field is common, good practice and part of Entity Framework conventions.
the key here is the int? allowing null

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.