Addig用户和我的Userrole表的角色 - 从未使用过列名称
我正在尝试向用户添加角色,并将用户ID和PROARID存储在用户表中。
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:关系“ releid1”列“用户”不存在
,但我不知道什么是ralesid1,这是什么来自...
这是模型:
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();
});
感谢您的事先提供帮助
I am trying to add Role to my User and store the UserID and the RoleID in the UsersRoles table.
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 "RoleId1" of relation "UsersRoles" does not exist
But I don't know what is RoleId1 and where this is coming from...
This is the model:
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; }
}
And this is what I have in the context:
modelBuilder.Entity<UserProfile>(b =>
{
b.HasOne(up => up.UserRole)
.WithMany(r => r.UserProfiles)
.HasForeignKey(up => up.RoleID)
.IsRequired();
});
Thanks for your help in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您在这里试图完成的工作是什么。用户和角色关系已在Aspindentity中可用
what is it that you are trying to accomplish here. User and role relations are already available in aspIdentity