2014-04-01 67 views
1

當我第一次發佈我的項目時,種子方法運行並將數據插入到表中。但是當我用更多的數據更改種子方法時,種子方法不起作用。Asp.net MVC種子方法不會在發佈時運行

And:Shoud我設置了false「AutomaticMigrationsEnabled」和「AutomaticMigrationDataLossAllowed」參數?

我的配置文件,如下:

internal sealed class Configuration : DbMigrationsConfiguration<ModulericaV1.Models.ApplicationDbContext> 
    { 
     public Configuration() 
     { 
      AutomaticMigrationsEnabled = true; 
      AutomaticMigrationDataLossAllowed = true; 

     } 

     protected override void Seed(ApplicationDbContext context) 
     { 
      this.AddUserAndRoles(); 
     } 


     bool AddUserAndRoles() 
     { 
      bool success = false; 

      var idManager = new IdentityManager(); 
      success = idManager.CreateRole("Admin"); 
      if (!success == true) return success; 

      success = idManager.CreateRole("HR_Admin"); 
      if (!success == true) return success; 

      success = idManager.CreateRole("HR_Visitor"); 
      if (!success) return success; 


      var newUser = new ApplicationUser() 
      { 
       UserName = "pascal", 
       FirstName = "umki", 
       LastName = "umkiii", 
       Email = "[email protected]" 
      }; 

      success = idManager.CreateUser(newUser, "Password1"); 
      if (!success) return success; 

      success = idManager.AddUserToRole(newUser.Id, "Admin"); 
      if (!success) return success; 

      return success; 
     } 
    } 

回答

0

你的遷移配置應該是這樣的:

public Configuration() 
    { 
     AutomaticMigrationsEnabled = true; 
     AutomaticMigrationDataLossAllowed = true; 
    } 

您可能還需要保存在年底改變。

context.SaveChanges(); 
1

如果您使用的是AutoMapper,則可能需要在Global.asax.cs文件中對其進行配置。 我只是用下面的行做到了:

var autoMapperConfig = new AutoMapperConfig(Assembly.GetExecutingAssembly()); 
autoMapperConfig.Execute(); 

,並設置「AutoMapperConfig」您可以使用代碼here

相關問題