当我第一次发布我的项目时,种子方法运行并将数据插入表中。但是当我用更多数据更改种子方法时,种子方法不起作用。
并且:Shoud我设置false" AutomaticMigrationsEnabled"和#34; 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 = "asdfads@asdas.com"
};
success = idManager.CreateUser(newUser, "Password1");
if (!success) return success;
success = idManager.AddUserToRole(newUser.Id, "Admin");
if (!success) return success;
return success;
}
}
答案 0 :(得分:1)
如果您使用的是AutoMapper,则可能需要在Global.asax.cs文件中对其进行配置。 我只是使用以下行来完成它:
var autoMapperConfig = new AutoMapperConfig(Assembly.GetExecutingAssembly());
autoMapperConfig.Execute();
并设置“AutoMapperConfig”,您可以使用代码here
答案 1 :(得分:0)
您的迁移配置应如下所示:
public Configuration()
{
AutomaticMigrationsEnabled = true;
AutomaticMigrationDataLossAllowed = true;
}
您可能还需要在最后保存更改。
context.SaveChanges();