Laser Continuous Deployment - First Code Seed Migration Issue (MVC 5)

I installed Continuous Deployment to Microsoft Azure (web app) using the ButBucket Git repository. Code First Migrations works well on my machine, it creates tables and seeds them, but when I sync the branch, the migration seed method doesn't start on Azure .

So Azure receives changes from BitBucket, creates tables as needed, but does not run the seed method (each table remains empty).

Can you suggest a solution to trigger the Seed method on Azure automatically when a new migration is applied (or every time Azure builds from BitBucket, if that's the only solution)?

Additional Information:

  • The MigrationHistory table contains migrations, so they were run.
  • I have set AutomaticMigrationsEnabled = true; but the problem remains
  • There is a web app on Azure that is built and ported and a SQL database that is referenced in the ConnectionString in the Web.config

Configuration.cs

internal sealed class Configuration : DbMigrationsConfiguration<MyInsidR.Models.ApplicationDbContext>
{
    public Configuration()
    {
        AutomaticMigrationsEnabled = true;
        ContextKey = "MyInsidR.Models.ApplicationDbContext";
    }

    protected override void Seed(ApplicationDbContext 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. E.g.
        //
        //    context.People.AddOrUpdate(
        //      p => p.FullName,
        //      new Person { FullName = "Andrew Peters" },
        //      new Person { FullName = "Brice Lambson" },
        //      new Person { FullName = "Rowan Miller" }
        //    );
        //

        context.Prophecies.AddOrUpdate(p => p.ID,
            new Prophecy() { ID = 1, Text = "Fűben iszogatós, sírva nevetős."}
        );

        context.Interesteds.AddOrUpdate(x => x.ID,
            new Interested() { ID = 1, Email = "teszt.elek@gmail.com", FirstName = "Elek", LastName = "Teszt", RegistrationDate = DateTime.Now }
        );

        var tag1 = new Tag() { ID = 1, Name = "Karaoke", ApplyTo = TagApplication.All, Type = TagType.Games };
        var tag3 = new Tag() { ID = 3, Name = "4 rooms", ApplyTo = TagApplication.All, Type = TagType.Misc };
        var tag4 = new Tag() { ID = 4, Name = "Helipad", ApplyTo = TagApplication.All, Type = TagType.Vip };

        context.Tags.AddOrUpdate(x => x.ID,
            tag1, tag3, tag4
        );

        var indicatorIcon1 = new IndicatorIcon() { ID = 1, VisualClass = IndicatorIcon.VisualClassType.Hidden, Name = "No Indicator Icon", Description = "Nothing special, just a regular place or event." };
        var indicatorIcon2 = new IndicatorIcon() { ID = 2, VisualClass = IndicatorIcon.VisualClassType.Fire, Name = "Hot", Description = "This place or event is very popular at the moment. There are big parties and a big fuss around it." };
        context.IndicatorIcons.AddOrUpdate(x => x.ID,
            indicatorIcon1, indicatorIcon2
        );

        AddUserAndRole(context);
    }

    bool AddUserAndRole(ApplicationDbContext context)
    {
        var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(context));
        var identityResult = roleManager.Create(new IdentityRole("Admin"));

        var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
        var user = new ApplicationUser()
        {
            UserName = "myinsidr@gmail.com",
        };
        identityResult = userManager.Create(user, "Qwertz1234!");
        if (identityResult.Succeeded == false)
            return identityResult.Succeeded;

        identityResult = userManager.AddToRole(user.Id, "Admin");
        return identityResult.Succeeded;
    }
}

      

(I found questions and solutions related to the seed method problem only for direct deployment from Visual Studio, but this is not how I would like.

There are also solutions using different SQL management projects, but I think the first code migrations inside an MVC project are the cleanest solution if it works like on my local machine)

+3


source to share


1 answer


I figured out how to run the Seed method, every server starts using this technique: http://romiller.com/2012/02/09/running-scripting-migrations-from-code/

Running a seed on every server start is pretty good for me as it will run after every Azure Continuous Deployment build. Of course it will work in other cases too, but my method is not too long, so it doesn't matter.

I put the following code in Global.asax -> Application_Start ():

var migrator = new DbMigrator(new Configuration());
migrator.Update();

      



how

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();
    GlobalConfiguration.Configure(WebApiConfig.Register);
    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
    RouteConfig.RegisterRoutes(RouteTable.Routes);
    BundleConfig.RegisterBundles(BundleTable.Bundles);

    // CODE FIRST MIGRATIONS
    #if !DEBUG
       var migrator = new DbMigrator(new Configuration());
       migrator.Update();
    #endif
}

      

What this means is that basically the first code migration is done on every server startup.

+2


source







All Articles