我试图将角色添加到我的用户并将UserID和RoleID存储在UsersRoles表中。
var userRole = await _roleManager.FindByNameAsync("User");
user.UserRole = userRole;
var result = await _userManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{ //until this point, everything goes well
UsersRoles ur = new UsersRoles();
ur.UserId = user.Id;
ur.RoleId = user.UserRole.Id;
unitOfWork.UsersRolesRepository.Insert(ur);
unitOfWork.Save(); // At this point I have an Exception:
42703: column "UsersRoles"不存在
但我不知道什么是RoleId1和这是从哪里来的…
这是模型:
public class UserProfile : IdentityUser
{
public string Email { get; set; }
public DateTime? Birthday { get; set; }
public string CountryName { get; set; }
public string RoleID { get; set; }
[ForeignKey("Id")]
public virtual UserRole UserRole { get; set; }
}
public class UserRole : IdentityRole
{
public virtual ICollection<UserProfile> UserProfiles { get; set; }
}
public class UsersRoles : IdentityUserRole<string>
{
public virtual UserProfile User { get; set; }
public virtual UserRole Role { get; set; }
}
这是我在上下文中的内容:
modelBuilder.Entity<UserProfile>(b =>
{
b.HasOne(up => up.UserRole)
.WithMany(r => r.UserProfiles)
.HasForeignKey(up => up.RoleID)
.IsRequired();
});
事先感谢您的帮助
你在这里想要完成什么?用户和角色关系在aspIdentity
中已经可用