none
Entity Framework model first migration RRS feed

  • Question

  • I have a Project where I use Entity Framework with a edmx-file to map the database. Now I have released my application to some customers and I need to update the customers database, because the model changed. I added one new column to a table.

    What is the best way to handle this?

    Thanks

    Andreas

    Monday, November 24, 2014 9:09 AM

Answers

  • Hello engenious,

    >> Can you discribe me who a diff script works with this Tools. I found no article.

    This tool actually is used to generate the POCO, mapping (configuration), and derived DbContext classes that are based on an existing database. It does not generate the update script directly. Since you already have a database, you could generate the code first model by:

    Right click the project(you could create a new one) and select Entity Framework –> Reverse Engineer Code First.

    Enter the information about an existing database(this database should be the original one means you do not add the new column and recommend you backup it) based on which you want to reverse engineer Code First

    For detail steps, you could check this article.

    For generating the update script, using the code first migration, in my site the command steps are:

    1.run enable-migrations

    2.open the context class, delete/comment the constructor:

    //static LINQDBContext()
    
            //{
    
            //    Database.SetInitializer<LINQDBContext>(null);
    
            //}
    
    
            //public LINQDBContext()
    
            //    : base("Name=LINQDBContext")
    
            //{
    
            //} 
    

    3. remove the connection string in web/app.config

    4. run add-migration InitCreate

    5. run update-database

    6. modify the model as adding a new column or other migrations

    7. run add-migration MigrationXXX

    8. run update-database –script. This step would generate the update script.

    In your case, if you just add one column, in actual, you could write the alter sql statement directly and send it to customers, my solution should be appropriate for lots of changes for the model.

    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.

    Wednesday, November 26, 2014 9:18 AM
    Moderator

All replies

  • Hello Andreas,

    >>What is the best way to handle this?

    As far as I know there still is no automatic migration for Entity framework model first and Migrations support only code first approach at the moment. This feature may be in the further release version.

    In your case, I think you need to send the update script for adding a column to customers and let customers execute the update script and for getting the update script, you could consider about this way:

    In your development environment, before updating your database, using the EF Power Tools to generates derived DbContext class, POCO classes, and Code First mapping classes for an existing database. Then enable the migration for this DbContext, adding a migration for adding the column you want, and running “update-database –script”, this would generate the update script.

    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.

    Tuesday, November 25, 2014 1:53 AM
    Moderator
  • Hello Fred,

    Is there a way to override a initialization method of the database and than add the missing column by code? Are the Power Tools work with VS 2013?

    Regards

    Andreas

    Tuesday, November 25, 2014 8:23 AM
  • Hello,

    >> Is there a way to override a initialization method of the database and than add the missing column by code?

    As far as I know, there is an automatic migrations, however, it is used for code first approach.

    >> Are the Power Tools work with VS 2013?

    Yes, it could work for Visual Studio 2013, 2012 and 2010 and with this tool.


    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, November 25, 2014 9:50 AM
    Moderator
  • Ok I installed the power Tools. Can you discribe me who a diff script works with this Tools. I found no article.

    Thanks Fred.

    Tuesday, November 25, 2014 12:53 PM
  • Hello engenious,

    >> Can you discribe me who a diff script works with this Tools. I found no article.

    This tool actually is used to generate the POCO, mapping (configuration), and derived DbContext classes that are based on an existing database. It does not generate the update script directly. Since you already have a database, you could generate the code first model by:

    Right click the project(you could create a new one) and select Entity Framework –> Reverse Engineer Code First.

    Enter the information about an existing database(this database should be the original one means you do not add the new column and recommend you backup it) based on which you want to reverse engineer Code First

    For detail steps, you could check this article.

    For generating the update script, using the code first migration, in my site the command steps are:

    1.run enable-migrations

    2.open the context class, delete/comment the constructor:

    //static LINQDBContext()
    
            //{
    
            //    Database.SetInitializer<LINQDBContext>(null);
    
            //}
    
    
            //public LINQDBContext()
    
            //    : base("Name=LINQDBContext")
    
            //{
    
            //} 
    

    3. remove the connection string in web/app.config

    4. run add-migration InitCreate

    5. run update-database

    6. modify the model as adding a new column or other migrations

    7. run add-migration MigrationXXX

    8. run update-database –script. This step would generate the update script.

    In your case, if you just add one column, in actual, you could write the alter sql statement directly and send it to customers, my solution should be appropriate for lots of changes for the model.

    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.

    Wednesday, November 26, 2014 9:18 AM
    Moderator
  • Thank you very much for your help. I will try it.

    Regards

    Andreas

    Friday, November 28, 2014 1:33 PM
  • Hello Andreas,

    Any update? I have marked my last reply as answer since I think it is helpful, if you think it provides no help, please unmark it.

    Thank you for your understanding and support.


    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, December 9, 2014 7:41 AM
    Moderator
  • Yes I tried it, but found another way to handle this. The Problem was that I released the database to some customers and when I did your way, then with code first all database tables had different names (without "set" in the name).

    Now I store a Version number in the database, and after initializing the database with the model, then I create or delete the neccesary tables and columns by an command:

    context.Database.ExecuteSqlCommand("...")

    Tuesday, December 9, 2014 9:16 AM