2014-04-01 27 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

相关问题