none
Code First: is it possible to configure Visual Studio to run a SQL Script after performing a Database Migration? RRS feed

  • Question

  • Hello everyone,

    I've used EF Code-First Migrations for an application in the past, I understand how it works.
    My question is more regarding the possibility of doing a "2 in 1" Database Migration + run a SQL script to populate the database afterwards.

    For example, I would like to add table "A" to my current SQL database. I also have the possible values that should be used to populate this table. Would it be possible to execute this script after the migration has finished?
    Or is this simply something EF doesn't support and has to be a separate act?


    Thanks in advance
    JB 

    JB

    Wednesday, February 19, 2014 5:19 PM

Answers

  • Hello,

    >> I also have the possible values that should be used to populate this table.

    Then these data should be seed data, however, it should not be in script. We need to add seed data in Seed() method. This method will be called after migrating to the latest version. Here I made an example:

    I added new table:

    public class HomeNew
    
        {
    
            public int ID { get; set; }
    
    
    
            public string Name { get; set; }
    
        }
    

    Add the possible data in Seed() method:

    protected override void Seed(P201402CF.S20.S20Context context)
    
            {
    
                //  This method will be called after migrating to the latest version.
    
    
    
                //  You can use the DbSet<T>.AddOrUpdate() helper extension method 
    
                //  to avoid creating duplicate seed data.
    
                context.HomeNews.AddOrUpdate(
    
                    new P201402CF.S20.HomeNew() { ID = 1, Name = "1" },
    
                    new P201402CF.S20.HomeNew() { ID = 2, Name = "2" },
    
                    new P201402CF.S20.HomeNew() { ID = 3, Name = "3" });
    
    
    
            }
    

    Then use the migrations to generate the table which will with possible data in Seed() method.

    For more information regarding Seed() method, please refer to link below:

    http://www.codeproject.com/Articles/504720/EntityplusFrameworkplusCodeplusFirstplusMigrations

    If I misunderstand, please let me know.

    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.

    • Marked as answer by SuperJB Thursday, February 20, 2014 8:22 AM
    Thursday, February 20, 2014 5:06 AM
    Moderator

All replies

  • Hello,

    >> I also have the possible values that should be used to populate this table.

    Then these data should be seed data, however, it should not be in script. We need to add seed data in Seed() method. This method will be called after migrating to the latest version. Here I made an example:

    I added new table:

    public class HomeNew
    
        {
    
            public int ID { get; set; }
    
    
    
            public string Name { get; set; }
    
        }
    

    Add the possible data in Seed() method:

    protected override void Seed(P201402CF.S20.S20Context context)
    
            {
    
                //  This method will be called after migrating to the latest version.
    
    
    
                //  You can use the DbSet<T>.AddOrUpdate() helper extension method 
    
                //  to avoid creating duplicate seed data.
    
                context.HomeNews.AddOrUpdate(
    
                    new P201402CF.S20.HomeNew() { ID = 1, Name = "1" },
    
                    new P201402CF.S20.HomeNew() { ID = 2, Name = "2" },
    
                    new P201402CF.S20.HomeNew() { ID = 3, Name = "3" });
    
    
    
            }
    

    Then use the migrations to generate the table which will with possible data in Seed() method.

    For more information regarding Seed() method, please refer to link below:

    http://www.codeproject.com/Articles/504720/EntityplusFrameworkplusCodeplusFirstplusMigrations

    If I misunderstand, please let me know.

    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.

    • Marked as answer by SuperJB Thursday, February 20, 2014 8:22 AM
    Thursday, February 20, 2014 5:06 AM
    Moderator
  • Thanks for the post!
    Although it would be great if EF allowed you to manage this per migration script, as opposed to doing it all in one specific place.


    JB

    JB

    Thursday, February 20, 2014 8:26 AM