'Addig User And Role to my UserRole table - Never used column name is missing

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



Solution 1:[1]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 bachu noyan