【发布时间】:2020-06-02 12:37:36
【问题描述】:
我是初学者,我在使用 Asp.Net Core 3 MVC。我正在尝试播种 AspNetUserRoles 表。用户和角色正常工作并且没有错误地播种,但 AspNetUserRoles 表为空。我创建了这样的种子类:
public class SeedData
{
private AppIdentityDbContext _context;
public SeedData(AppIdentityDbContext context)
{
_context = context;
}
public async void Initialize()
{
var user = new ApplicationUser
{
UserName = "progmd@mail.ru",
NormalizedUserName = "progmd@mail.ru",
Email = "progmd@mail.ru",
NormalizedEmail = "progmd@mail.ru",
City = "SuperAdmin",
EmailConfirmed = true,
LockoutEnabled = false,
SecurityStamp = Guid.NewGuid().ToString()
};
var roleStore = new RoleStore<IdentityRole>(_context);
if (!_context.Roles.Any(r => r.Name == "admin"))
{
await roleStore.CreateAsync(new IdentityRole { Id = "b562e963-6e7e-4f42-9339-1235b1234qw5", Name = "admin", NormalizedName = "admin" });
}
if (!_context.Users.Any(u => u.UserName == user.UserName))
{
var password = new PasswordHasher<ApplicationUser>();
var hashed = password.HashPassword(user, "111111Test");
user.PasswordHash = hashed;
var userStore = new UserStore<ApplicationUser>(_context);
await userStore.CreateAsync(user);
await userStore.AddToRoleAsync(user, "admin");
}
IdentityUserRole<string> ur = new IdentityUserRole<string>();
ur.RoleId = "b562e963-6e7e-4f42-9339-1235b1234qw5";
ur.UserId = user.Id;
//await _context.UserRoles.AddAsync(ur);
await _context.SaveChangesAsync();
}
}
我该如何解决这个问题?
【问题讨论】:
标签: c# entity-framework-core asp.net-core-mvc