簡體   English   中英

Azure持續部署 - 代碼優先遷移種子問題(MVC 5)

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

我使用ButBucket Git倉庫在Microsoft Azure(Web App)中設置了持續部署 Code First Migrations在我的計算機上運行良好,它創建表並播種它們,但是當我同步分支時, 遷移的種子方法不在Azure上運行

因此,Azure從BitBucket獲取更改,根據需要創建表,但不運行種子方法(每個表都保持為空)。

您是否可以建議在應用新遷移時自動在Azure上運行Seed方法的解決方案 (或者每次Azure從BitBucket構建之后,如果這是唯一的解決方案)?

附加信息:

  • MigrationHistory表包含遷移,因此它們已運行。
  • 我設置了AutomaticMigrationsEnabled = true; 但問題仍然存在
  • 在Azure上,有一個構建和遷移的Web App,以及一個在Web.config的ConnectionString中引用的SQL數據庫。

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/

在每個服務器啟動時運行Seed對我來說非常好,因為它將在Azure Continuous Deployment的每次構建之后運行。 當然它也會在其他情況下運行,但我的方法不會太長,所以沒關系。

我將以下代碼放到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
}

這樣做基本上是在每個服務器啟動時運行Code First Migration。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM