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

本文关键字:问题 MVC 部署 代码 Azure 迁移 | 更新日期: 2023-09-27 18:03:29

我已经在Microsoft Azure (Web App)中使用ButBucket Git repo设置了持续部署。代码优先迁移在我的计算机上工作得很好,它创建表并播种它们,但是当我同步分支时,迁移的种子方法不在Azure上运行

因此Azure从BitBucket获取更改,根据需要创建表,但不运行seed方法(每个表保持空)。

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

附加信息:

  • MigrationHistory表包含迁移,因此它们被运行。
  • 我已经设置了AutomaticMigrationsEnabled = true;但问题仍然存在
  • 在Azure上有一个构建和迁移的Web应用程序,以及一个SQL数据库,在Web.config
  • 中的ConnectionString中引用。

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项目内的代码首先迁移是最干净的解决方案,如果它在我的本地机器上工作)

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

我已经发现如何运行种子方法,每个服务器开始使用这种技术:http://romiller.com/2012/02/09/running-scripting-migrations-from-code/

在每次服务器启动时运行Seed对我来说非常好,因为它将在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
}

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