none
Code First migrations switching columns. RRS feed

  • Question

  • On my current project we are using Entity Framework 6.1.1 Code First on a Sql 2012 database, Visual Studio 2013 and TFS 2012. The team consist of 4 developers each working on their local database. Though each developer can create and check in his own migrations, it is mostly me that is creating the data model and migrations.

    Since a few weeks we are experiencing a very weir behavior. I create a migration and apply it to my local database and also to the database on the development server and check all of the code into TFS. Somethimes when one of the other developers gets my changes and executes Update-Database, he gets an exception stating “Unable to update database to match the current model because there are pending changes and automatic migration is disabled”. If he executes an Add-Migration command, he will get a migration that only wants to switch 2 columns on a table. One of these migrations contained the following Up method:

    public override void Up()

    {

             RenameColumn(table: "dbo.ConfiguredStructuralTherapyAction", name: "AllowedDeviationForPlanningBeforeId", newName: "__mig_tmp__0");

            RenameColumn(table: "dbo.ConfiguredStructuralTherapyAction", name: "AllowedDeviationForPlanningAfterId", newName: "AllowedDeviationForPlanningBeforeId");

            RenameColumn(table: "dbo.ConfiguredStructuralTherapyAction", name: "__mig_tmp__0", newName: "AllowedDeviationForPlanningAfterId");

            RenameIndex(table: "dbo.ConfiguredStructuralTherapyAction", name: "IX_AllowedDeviationForPlanningAfterId", newName: "__mig_tmp__0");

            RenameIndex(table: "dbo.ConfiguredStructuralTherapyAction", name: "IX_AllowedDeviationForPlanningBeforeId", newName: "IX_AllowedDeviationForPlanningAfterId");

            RenameIndex(table: "dbo.ConfiguredStructuralTherapyAction", name: "__mig_tmp__0", newName: "IX_AllowedDeviationForPlanningBeforeId");

            }

     

    If the developer executes this migration to the database on the development server or checks it into TFS, I will be forced to create a migration that executes exactly the opposite afterwards. This problem is not only causing a lot of problems between developers, but also when publishing to the development and test environments.

    Last week it got even worse. I created a new migration, applied it to a database on the server and published my application on that server. When I try to run the application on the server, I get that same exception “Unable to update database to match the current model because there are pending changes and automatic migration is disabled”. I checked several times if I missed a migration, if the connectionstring is pointing to the correct database and if the latest version of the assembly was published, but could not find anything wrong.

    This morning I realized that this problem surfaced after we switched from Entity Framework 6.0.0 to 6.1.1. It may be a coincidence, but maybe not.

    Is there anybody who observed the same behavior and did you get it solved?

    Monday, August 4, 2014 8:52 AM

All replies

  • Hello,

    >>Somethimes when one of the other developers gets my changes and executes Update-Database…

    From my understanding, you update the model and check your changed model to server and then the developer download the changed model to cover his original one, is it right? After the developer use the “update-database” to update the changes to his local database, does he got a response as “No pending explicit migrations.“ and the following message is “Unable to update database to match the current model because there are pending changes and automatic migration is disabled”?

    If he gets, then this is by designed, since the develops model is changed which does not match his current local database, we need to firstly add a migration and then use update-database command to apply changes to database.

    >>If he executes an Add-Migration command, he will get a migration that only wants to switch 2 columns on a table

    What is the expected migrations you want? Is current migration incorrect?

    >>When I try to run the application on the server, I get that same exception “Unable to update database to match the current model because there are pending changes and automatic migration is disabled”

    This is very strange, because this error shows the model in your published application does not match the connected database while the application is published from your local environment.

    Could you please try to downgrade the Entity Framework to 6.0.0.0 to check whether it is caused by upgrading this dll?

    I not sure if there is something wrong occurs during the publish process, you could try to run your local application on your server directly to see whether it could work fine.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, August 5, 2014 3:27 AM
    Moderator
  • Thanks for your reply. I have been on holliday for a few weeks and that is why I did not reply earlier.

    In order to try to solve the problem or to get more insight we did the following:

    • Create a new empty database
    • Execute all the migrations on it by one of my colleagues
    • Check in the latest version of the code on TFS
    • Check out the lates version of the code on my computer 
    • Execute add-migration on the same database
    • The same migration shown in my earlier post gets created

    One would expect that an empty migration would be created since it is the same new database with all migrations on it and we are executing the add-migration command on exact the same code base. It is also weird the migration only switched two columns.

    When I execute add-migration on another colleague's machine, he gets an empty migration as expected.


    Friday, August 29, 2014 9:26 AM
  • Hello JENTHO,

    >>When I execute add-migration on another colleague's machine, he gets an empty migration as expected.

    You mean that others would get an empty migration as expect while it does not work for you only. This is strange because you use the TFS to get the same source.

    Is the migration in your original post one of these migrations and it is the last one? Although it has three renamed operations, it seems to only rename one column, is that column specific as it is an identity column?

    Best Regards,

    Fred.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, August 29, 2014 9:52 AM
    Moderator
  • Hi Fred,

    Yes, the others always get an emtpy migration and it does not work for me. If I check in this migration in TFS and they execute it, they get a new migration that reverses the one I checked in.

    Yes, the migration in the original post is the particular migration I get. It seems to me the migration is only switching the order of the 2 columns. Both columns are of type UNIQUEIDENTIFIER and are Foreign Keys to the same table.

    Best Regards,

    Tom

    Friday, August 29, 2014 2:38 PM
  • Hello Tom,

    After this step “Check out the lates version of the code on my computer”, have you checked that the property “AllowedDeviationForPlanningBeforeId” is changed to “AllowedDeviationForPlanningAfterId” on your computer? I am wondering if the property is not changed to the last version one.

    Best Regards,

    Fred.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, September 1, 2014 8:10 AM
    Moderator
  • Hi Tom and Fred,

    I've only just upgraded from EF 6.1.0 to 6.1.1 and noticed this behaviour as well. Did any of you already got this solved? Or should I pitch in with some background info I have wrt the project I'm working on?

    I also opened an issue here; https://entityframework.codeplex.com/workitem/2500

    Thursday, September 11, 2014 12:06 PM