2

EF Migrations can't seem to detect my changes to the model, as it just generates an empty Up() / Down() when running Add-Migration.

The database is not out of sync with my migrations.

Migration file: Migration file

__migrationhistory table: __migrationhistory table

Before

public class House {
    public virtual ICollection<Person> UsedBy { get; set; }
}

public class Car {
    // ...
}

public class Person {
    public int? UsingId { get; set; }
    public virtual House Using { get; set; }
}

Mapping

this.HasOptional(t => t.Using)
                .WithMany(t => t.UsedBy)
                .HasForeignKey(t => t.UsingId);

After

public class House {
    // removed
}

public class Car {
    // added
    public virtual ICollection<Person> UsedBy { get; set; }
}

public class Person {
    public int? UsingId { get; set; }
    // changed type
    public virtual Car Using { get; set; }
}

Mapping

this.HasOptional(t => t.Using)
                .WithMany(t => t.UsedBy)
                .HasForeignKey(t => t.UsingId);
Snæbjørn
  • 10,322
  • 14
  • 65
  • 124

1 Answers1

2

As already said in my comment I could reproduce this, but renaming the Using property also helped.
If that is not an option you could use the following code added manually to the migration:

public override void Up()
{
    DropForeignKey("dbo.People", "UsingId", "dbo.Houses");
    AddForeignKey("dbo.People", "UsingId", "dbo.Cars", "Id");
}

public override void Down()
{
    DropForeignKey("dbo.People", "UsingId", "dbo.Cars");
    AddForeignKey("dbo.People", "UsingId", "dbo.Houses", "Id");
}

This is based on what Add-Migration creates when renaming the property.

Christoph Fink
  • 22,727
  • 9
  • 68
  • 113
  • Perhaps we should report this bug :) – Snæbjørn Oct 08 '14 at 15:07
  • Something funky is going on when I manually alter the `Up() / Down()` methods. I'm getting the following error: Table 'mydb.dbo.people' doesn't exist. I checked the database and it's there. When I empty the `Up() / Down()` everything works fine again... – Snæbjørn Oct 09 '14 at 12:09
  • Renaming the property results in the same "Table 'mydb.dbo.people' doesn't exist." error :( – Snæbjørn Oct 09 '14 at 12:17
  • Thats strange. Did not happen in my test project. Try to genreate the SQL script an execute it manually via SSMS? – Christoph Fink Oct 09 '14 at 12:21
  • Perhaps it's because I'm renaming the table with `this.ToTable("...")`. I'll have to test that out in an isolated case. Thanks for the help btw :) – Snæbjørn Oct 09 '14 at 12:28
  • Hmm seems it's now MySQL that the problem. I generated the SQL script and tried to run it on the server but got the same error. Found others getting the same error http://stackoverflow.com/questions/15869476/error-code-1146-but-table-does-exist so moving forward-ish :/ – Snæbjørn Oct 09 '14 at 12:43
  • MySQL? Don't you mean MS SQL? – Christoph Fink Oct 09 '14 at 12:50
  • We've gone a bit off topic :) But I found the solution here: http://bugs.mysql.com/bug.php?id=69649 I had to add `CodeGenerator = new MySql.Data.Entity.MySqlMigrationCodeGenerator();` (had the others). So Renamed the properties and looked at what was generated (as suggested) then edited them manually to fit and BAM great success! – Snæbjørn Oct 09 '14 at 13:32
  • I confirm this is working also in VS 2017 (even tough it's not a VS issue) – Liquid Core Aug 06 '18 at 08:20