2015-07-19 52 views
3

我已經使用ButBucket Git回購在Microsoft Azure(Web App)中設置了連續部署。 Code First Migrations在我的計算機上運行良好,它創建表並將它們播種,但是當我同步分支時,遷移的種子方法未在Azure上運行。Azure持續部署 - 代碼優先遷移播種問題(MVC 5)

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

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

附加信息:

  • MigrationHistory表包含遷移,所以它們運行。
  • 我已經設置了AutomaticMigrationsEnabled = true;但問題仍然存在
  • 在Azure上還有這是建立和遷移的Web應用程序,而這是在ConnectionString引用Web.config中

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 = "[email protected]", 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 = "[email protected]", 
     }; 
     identityResult = userManager.Create(user, "Qwertz1234!"); 
     if (identityResult.Succeeded == false) 
      return identityResult.Succeeded; 

     identityResult = userManager.AddToRole(user.Id, "Admin"); 
     return identityResult.Succeeded; 
    } 
} 
SQL數據庫

(我發現只有從Visual Studio直接部署的種子方法問題相關的問題和解決方案,但這不是我想要去的方式。但我認爲MVC項目中的代碼第一次遷移是最乾淨的解決方案,如果它在我的本地計算機上工作)

+0

你解決了這個問題嗎? – erwineberhard

+0

是的,但我不得不修改代碼,而不是我以前的概念來修復它在Azure UI或某些配置文件中。詳情請見我的回答! –

回答

2

我已經找到了如何運行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 
} 

這裏做的事情在每個服務器啓動基本運行的代碼第一次遷移。

+0

是的,這是解決方案。 – erwineberhard

+0

當我嘗試此解決方案時,在部署之後出現以下異常: 數據庫中已有一個名爲'PuzzleHunts'的對象。 異常詳細信息:System.Data.SqlClient.SqlException:數據庫中已有一個名爲'PuzzleHunts'的對象。 我想它試圖創建已經存在的表。我想堅持現有的表並「升級」模式而不重新創建它們。有關如何通過持續部署來實現這一點的任何建議? – DanJosef

+0

上述解決方案僅自動運行遷移。結果將與在包管理器控制檯中使用Update-Database運行遷移完全相同。 https://msdn.microsoft.com/en-us/data/dn579398.aspx 您遇到的問題是由於不正確的遷移文件。打開你的Migration文件夾並修改你的.cs遷移文件,因爲在某處應該有一個額外的CreateTable(...)方法。 也許你已經在dev計算機上生成了你的遷移文件(帶有Add-Migration),其中數據庫沒有包含與你的服務器相反的表。 –