Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi. I am trying for a couple of days now and just cannot make it work. It seems like pretty obvious task, however.

public class UserRole : IdentityUserRole<string>
{
    public virtual User User { get; set; }
    public virtual Role Role { get; set; }
}

public class User : IdentityUser
{
    public virtual ICollection<UserRole> UserRoles { get; set; }

}

public class Role : IdentityRole
{
    public virtual ICollection<UserRole> UserRoles { get; set; }
}


public class IdentityContext : IdentityDbContext<User, Role, string>
{
    public IdentityContext(DbContextOptions<IdentityContext> options) : base(options)
    {
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        modelBuilder.Entity<User>()
            .HasMany(e => e.UserRoles)
            .WithOne()
            .HasForeignKey(e => e.UserId)
            .IsRequired()
            .OnDelete(DeleteBehavior.NoAction);
}


services
    .AddIdentity<User, Role>(options =>
    {
        // Minimal password requirements
        options.Password.RequireDigit = false;
        options.Password.RequiredLength = 1;
        options.Password.RequireUppercase = false;
        options.Password.RequiredUniqueChars = 0;
        options.Password.RequireNonAlphanumeric = false;
        options.Password.RequireLowercase = false;
        options.User.RequireUniqueEmail = true;
    })
    .AddEntityFrameworkStores<IdentityContext>()
    .AddDefaultTokenProviders();


What I have tried:

I just want for a start to get join table joined to a user object, but:
var q = userManager.Users.Include(x => x.UserRoles).ToList();

q.UserRoles count is zero.
I have tried everything what is available on the net, nothing works in my case. UserRoles join table has always zero roles. I have one user and one role, and user is assigned to this role.
Please help

P.S. I can see that
IdentityDbContext
already is doing all required initialisation in its
OnModelCreating
.
If I change:
public sealed class User : IdentityUser
{
    // public ICollection<UserRole> UserRoles { get; set; }
    public ICollection<IdentityUserRole<string>> UserRoles { get; set; }

}

Then here:
<pre>var q = userManager.Users.Include(x => x.UserRoles).ToList();

I have UserRoles object, which has one item. But I cannot do further includes to get actual Role object.
Posted
Updated 25-Nov-20 21:30pm
v3
Comments
Richard Deeming 26-Nov-20 4:52am    
Your model builder looks slightly off:
modelBuilder.Entity<UserRole>()
    .HasOne(e => e.User)
    .WithMany(e => e.UserRoles)
    .HasForeignKey(e => e.UserId)
    .OnDelete(DeleteBehavior.Cascade);

modelBuilder.Entity<UserRole>()
    .HasOne(e => e.Role)
    .WithMany(e => e.UserRoles)
    .HasForeignKey(e => e.RoleId)
    .OnDelete(DeleteBehavior.Cascade);


Beyond that, you need to check your database to make sure the UserRoles table has the expected rows in it. If the table is empty, or the foreign key columns are not set properly, then you'll need to check the code that creates the user.
csrss 26-Nov-20 5:28am    
Nope, this does not work.
Richard Deeming 26-Nov-20 5:33am    
Have you checked the database tables to make sure the expected data exists?

Try logging the raw SQL query to see if there's anything obviously wrong with it:
How To Access SQL Generated By Entity Framework Core 3 – ChristianFindlay.com[^]
csrss 26-Nov-20 5:38am    
Yes, I have checked. The only thing which is odd to me, is:
1. In AspNetRoles there is a column Discriminator which is set to "Role"
2. In AspNetUserRoles there is a same column Discriminator which is set to "IdentityUserRole<string>".
Maybe because of this it is failing. Anyways, I have used different design where joining Collection<identityuserrole<string>> to user is sufficient, so I just leave it like this for now.
Richard Deeming 26-Nov-20 5:42am    
Sounds like you've got a TPH hierarchy set up:
Inheritance - EF Core | Microsoft Docs[^]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900