:::: MENU ::::

Fixing Asp.Net Core Identity Navigation Properties in .Net Core 2.0

There are few breaking changes in Asp.Net Core Identity in net core 2.0. One of the major change is removing navigation properties from Identity entities like IdentityUser, IdentityRole.

AspNet core team mentioned how to reimplement navigation properties in identity entities in net core 2.0 here. I have done exactly like that but still unable to get data in navigation properties. I googled for this issue but didn’t find much then I tried using Include extension method and it works.

If you are new to Include method then first check its documentation here.

Here is how I made it work.

In this example, I will take IdentityRole but it is same for IdentityUser.

Let’s add navigation properties in IdentityRole. In my case, I build TenantRole class which inherits IdentityRole<Key> class along with my custom properties.

Note: TenantRoleClaim inherits IdentityRoleClaim<Key> along with my custom properties.

Let’s add a relation between entities in OnModelCreating method.

Now we are finished with entity configuration let’s try to get some data from navigation properties using Include extension method.

My requirement was I need to get total no. of users of each role.
Code:

Now I can get users associated with the role and its total count too.

Let me know if you have any better approach.