gpt4 book ai didi

c# - Azure 持续部署 - Code First 迁移播种问题 (MVC 5)

转载 作者:太空狗 更新时间:2023-10-29 14:12:48 25 4
gpt4 key购买 nike

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

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

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

其他信息:

  • MigrationHistory 表包含迁移,因此它们已运行。
  • 我已经设置了AutomaticMigrationsEnabled = true;但问题依然存在
  • 在 Azure 上,有一个已构建和迁移的 Web 应用程序,以及一个在 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 = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="bacedfc9c0ce94dfd6dfd1faddd7dbd3d694d9d5d7" rel="noreferrer noopener nofollow">[email protected]</a>", 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 = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="204d59494e5349445260474d41494c0e434f4d" rel="noreferrer noopener nofollow">[email protected]</a>",
};
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 项目内的代码优先迁移是最干净的解决方案(如果它像在我的本地计算机上一样工作)

最佳答案

我已经找到了如何在每个服务器开始使用此技术时运行种子方法: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
}

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

关于c# - Azure 持续部署 - Code First 迁移播种问题 (MVC 5),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31500431/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com