none
Bug Report: EF Migrations DateTime Key as ForeignKey, Migrate to Int Key - Breaks Migrations RRS feed

  • Question

  • Create a Model like:

    public class Class1 {
        public virtual ICollection<Class2> Class2s { get; set; }
    public virtual Class2 CurrentClass2 { get; set; } } public class Class2 {     [Key]     public DateTime From { get; set; } }

    Run update-database to get things going then change the Model:

    public class Class1 { public virtual ICollection<Class2> Class2s { get; set; }
    public virtual Class2 CurrentClass2 { get; set; }
    }

    public class Class2 { public int Id { get; set; } public DateTime From { get; set; } }

    So Class2 now has changed its PK, which is going to affect Class1's FK pointing to it. Run update-database for fireworks.

    You'll get an exception complaining that the type of the FK does not match the type of the new PK, and won't be able to migrate anymore from here.

    I was able to workaround this by first commenting out the new ID field and the FK relationships between the tables and updating all related databases, then uncommenting the ID field and running update again on all DBs, and finally uncommenting the relationship properties and running update one last time.

    Monday, January 14, 2013 9:59 AM

All replies

  • Thanks for the bug report, I've created an issue for it here: http://entityframework.codeplex.com/workitem/776

    The team will investigate and report status there, rather than here on the forums.


    We are seeing a lot of great Entity Framework questions (and answers) from the community on Stack Overflow. As a result, our team is going to spend more time reading and answering questions posted on Stack Overflow. We would encourage you to post questions on Stack Overflow using the entity-framework tag. We will also continue to monitor the Entity Framework forum.

    Monday, January 14, 2013 6:33 PM
    Moderator
  • Perfect, thank you.

    There's not really an "answer" to be had here - I already found the workaround - so it isn't suitable for the StackOverflow Question & Answer format (as the Mods will passionately tell you should you violate said format...). Just a bug.

    It appears the proper place to report such bugs are there on the EF Codeplex site, thank you very much for creating the bug for me and I'll go direct there in the future - I come across a lot of EF Migrations SQL generation issues so this will hopefully be a productive place to make those known - maybe next time I can assemble a test case to help repro even.

    Monday, January 14, 2013 9:56 PM
  • No problems, Codeplex is where the EF team keep all of their work backlog. Thanks again for the report, the more people reporting issues the better.


    We are seeing a lot of great Entity Framework questions (and answers) from the community on Stack Overflow. As a result, our team is going to spend more time reading and answering questions posted on Stack Overflow. We would encourage you to post questions on Stack Overflow using the entity-framework tag. We will also continue to monitor the Entity Framework forum.

    Monday, January 14, 2013 10:15 PM
    Moderator