Azure 持续部署 - 代码优先迁移播种问题 (MVC 5)

Azure Continuous Deployment - Code First Migration seeding issue (MVC 5)

我已经使用 ButBucket Git 存储库在 Microsoft Azure(Web 应用程序)中设置了 持续部署。 Code First Migrations 在我的计算机上运行良好,它创建 tables 并为它们播种,但是当我同步分支时,迁移的种子方法不是 运行 在 Azure.

因此 Azure 从 BitBucket 获取更改,根据需要创建 tables,但不 运行 种子方法(每个 table 都为空)。

您能否建议一个解决方案 运行 Azure 上的 Seed 方法在应用新迁移时自动 (或者每次 Azure 从 BitBucket 构建之后,如果是的话)唯一的解决方案)?

附加信息:

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;
    }
}

(我发现与种子方法问题相关的问题和解决方案仅用于从 Visual Studio 直接部署,但这不是我想要的方式。

也有使用不同 SQL 管理项目的解决方案,但我认为 MVC 项目内部的代码优先迁移是最干净的解决方案,如果它像在我的本地机器上一样工作的话)

我已经找到如何 运行 每个服务器开始使用此技术的 Seed 方法:http://romiller.com/2012/02/09/running-scripting-migrations-from-code/

运行 在每次服务器启动时设置种子对我来说非常好,因为它会在 Azure 持续部署的每次构建后 运行。当然其他情况下也会运行,不过我的方法不会太长,所以无所谓。

我把下面的代码放到Global.asax --> Application_Start():

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

作为

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
}

这基本上是 运行在每次服务器启动时执行代码优先迁移。