EF Core 2 How To Include Roles Navigation Property On IdentityUser?

asp.net-core asp.net-identity entity-framework-core


I am setting up a new project using EF Core 2, and I need to have a navigation property on the IdentityUser so when I query for a user I can include(x => x.Roles) and get the Roles the user is in.

This post on Github has some ideas, but I have tried each one and all cause issues, by creating new/duplicate fields on the Identity tables or cause issues with migrations. And no official comment from anyone in the EF team.


I was wondering if anyone has this working correctly? And could share their EF DB mappings and models.

12/12/2017 7:26:07 AM

Accepted Answer

See the documentation for 'Migrating Authentication and Identity to ASP.NET Core 2.0', specifically the section 'Add IdentityUser POCO Navigation Properties':

The Entity Framework (EF) Core navigation properties of the base IdentityUser POCO (Plain Old CLR Object) have been removed. If your 1.x project used these properties, manually add them back to the 2.0 project:

/// <summary>
/// Navigation property for the roles this user belongs to.
/// </summary>
public virtual ICollection<IdentityUserRole<int>> Roles { get; } = new List<IdentityUserRole<int>>();

To prevent duplicate foreign keys when running EF Core Migrations, add the following to your IdentityDbContext class' OnModelCreating method (after the base.OnModelCreating(); call):

protected override void OnModelCreating(ModelBuilder builder)
    // Customize the ASP.NET Identity model and override the defaults if needed.
    // For example, you can rename the ASP.NET Identity table names and more.
    // Add your customizations after calling base.OnModelCreating(builder);

        .HasMany(e => e.Roles)
        .HasForeignKey(e => e.UserId)


The above will only satisfy the task of accessing the role Ids held against a user via the IdentityUserRole link table. To access the role entity itself via a navigation property, you would need to add another navigation property (this time against an entity inheriting from IdentityUserRole). See the steps below:

  1. Modify the Roles navigation property on your IdentityUser entity as follows:
public virtual ICollection<UserRole> Roles { get; set; } = new List<UserRole>();
  1. Create the UserRole entity referenced above:
public class UserRole : IdentityUserRole<int>
    public virtual IdentityRole<int> Role { get; set; }
  1. Construct the mapping for UserRole as follows:
    .HasOne(e => e.Role)
    .HasForeignKey(e => e.RoleId)
  1. You can then retrieve entities (with the navigation property populated) as follows:
User user = context.Set<User>()
    .Include(u => u.Roles)
    .ThenInclude(r => r.Role)


  • As this is loading another side of a many-to-many relatiohship, this may result in more than one call to the database (see N+1 problem).
  • As you are creating a new entity that inherits from IdentityUserRole you will need to migrate or re-create the database.
  • If you want to use this navigation property with UserManager or RoleManager you will need to use the long-form overload of AddUserStore() and AddRoleStore in your startup class, e.g.
services.AddIdentity<User, IdentityRole<int>>()
    .AddUserStore<UserStore<User, IdentityRole<int>, SqlContext, int, IdentityUserClaim<int>, UserRole, IdentityUserLogin<int>, IdentityUserToken<int>, IdentityRoleClaim<int>>>()
    .AddRoleStore<RoleStore<IdentityRole<int>, SqlContext, int, UserRole, IdentityRoleClaim<int>>>()
12/13/2017 10:37:00 AM

Popular Answer

I fetch roles by custom query and might be helpful.

var roles = (from role in _dbContext.Roles
                let userRoles = _dbContext.UserRoles.Where(ur => ur.UserId == user.Id).Select(ur => ur.RoleId)
                where userRoles.Contains(role.Id)
                select role

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow