I’m using EntityFrameworkCore 8.0.6 and Microsoft.Identity.Core 8.0.6.
I have a extended the models for user and roles
public class Role : IdentityRole<int>
{
...
public string? Description { get; set; }
public virtual ICollection<User>? Users { get; set; } = new List<User>();
}
and
public class User : IdentityUser<int>
{
...
public virtual ICollection<Role>? Roles { get; set; } = new List<Role>();
}
This is my db context:
public class ApiDbContext : IdentityDbContext<User, Role, int>
{
public override DbSet<User> Users { get; set; }
public override DbSet<Role> Roles { get; set; }
public ApiDbContext(DbContextOptions<ApiDbContext> options) : base(options) { }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<IdentityUserRole<int>>().ToTable("AspNetUserRoles");
This is creating numerous problems for me. Not only does it create the AspNetUserRoles table it’s creating a duplicate table called RoleUsers. And because of this when I query the db to get a user with roles the roles are always empty:
User? user = await _context.Users
.Include(u => u.Roles)
.FirstOrDefaultAsync(u => u.Id == request.Id);
I would simply like to get a user with a list of their list of Role objects populated, but I’m struggling to get Entity Framework to work with Microsoft Identity.
I’ve tried various combinations in the db context to map the table but IdentityUserRole doesn’t know what a Role or User is so having trouble mapping it.
user25604856 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.