Navigation properties in IdentityUserRole class

Apr 22, 2014 at 3:50 PM
Edited Apr 22, 2014 at 3:50 PM
In Microsoft.AspNet.Identity.EntityFramework v1.0, the IdentityUserRole class looks like this:
    public class IdentityUserRole
    {
        public IdentityUserRole();

        public virtual IdentityRole Role { get; set; }
        public virtual string RoleId { get; set; }
        public virtual IdentityUser User { get; set; }
        public virtual string UserId { get; set; }
    }
In 2.0, it looks like this:
    public class IdentityUserRole
    {
         public IdentityUserRole()
         public virtual TKey RoleId {get; set; }
         public virtual TKey UserId {get; set;}
    }
Why are the IdentityRole and IdentityUser navigation properties no longer there? After upgrading, my application breaks in code like the following, where I am trying to get the role based off the ApplicationUser entity:
ApplicationUser user = webSecurityService.Login(model.UserName, model.Password);
if( user.Roles.FirstOrDefault().Role.Name == "csr" )
{
   //direct to csr landing page
}
else if( user.Roles.FirstOrDefault().Role.Name == "user" )
{
  //direct to user landing page
}
Is there a better way to check a user's role? What am I doing wrong?

I think I am going to hold off on upgrading for now, but I'd like to know what I am doing wrong so I can fix it when I have time. Thanks
Developer
Apr 22, 2014 at 6:58 PM
@pgaule: This is a known change in 2.0 since we now support generic primary keys. For all roles and user operations use the UserManager class instead of working with the user and role object directly
Apr 22, 2014 at 7:37 PM
Well, the reason I am using the entities directly is so I can do eager loading. For instance, right now I am doing the following:
      public ApplicationUser GetUser(string id)
        {
            return _uow.RepositoryFor<ApplicationUser>()
                .GetWhere(u => u.Id == id)
                .Include(a => a.Roles.Select(r => r.Role))
                .FirstOrDefault();
        }
This will eagerly load the related roles as part of a single database query. If I use the UserManager.IsInRole(userId,RoleName) method, won't it make another roundtrip to the database? It seems that forcing people to use the UserManager and RoleManager doesn't promote for very efficient database access, unless there is a way to eagerly load the roles through the standard usermanager interface?
Developer
Apr 23, 2014 at 1:43 AM
Can you try setting EnableLazyLoading on the DBContext to false ?