我使用ButBucket Git repo在Microsoft Azure(Web App)中设置了持续部署。 Code First Migrations在我的计算机上运行良好,它可以创建表并为它们播种,但是当我同步分支时,迁移的种子方法不能在Azure上运行。
所以Azure从BitBucket获取更改,根据需要创建表,但不运行种子方法(每个表都保持为空)。
您是否可以建议在应用新迁移时自动在Azure上运行Seed方法的解决方案(或者每次从BitBucket构建Azure后,如果这是唯一的解决方案)?
其他信息:
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项目中的代码首次迁移是最干净的解决方案,如果它在本地计算机上工作的那样)
答案 0 :(得分:2)
我已经找到了如何运行Seed方法,每个服务器都开始使用这种技术:http://romiller.com/2012/02/09/running-scripting-migrations-from-code/
在每个服务器启动时运行Seed对我来说非常好,因为它将在Azure Continuous Deployment的每次构建之后运行。当然它也会在其他情况下运行,但我的方法不会太长,所以没关系。
我将以下代码放到Global.asax - &gt;的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。