Addig用户和我的Userrole表的角色 - 从未使用过列名称

发布于 2025-01-24 18:15:30 字数 1642 浏览 0 评论 0原文

我正在尝试向用户添加角色,并将用户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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

触ぅ动初心 2025-01-31 18:15:30

您在这里试图完成的工作是什么。用户和角色关系已在Aspindentity中可用

what is it that you are trying to accomplish here. User and role relations are already available in aspIdentity

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文