ThunD3eR
ThunD3eR

Reputation: 3456

Identity: Why is user.roles empty?

I have a method that gets all the users that i have in my db, simply put i do this:

var allUsers = context.Users.ToList();

What i can't figure it out is that when i debug the roles property it is empty:

enter image description here

but in dbo.UserRoles:

enter image description here

What am I missing here?

EDIT:

My registration method:

    public async Task<ActionResult> Register(RegisterViewModel model)
    {
        if (ModelState.IsValid)
        {
            var user = new ApplicationUser { UserName = model.Email, Email = model.Email, FirstName = model.FirstName, LastName = model.LastName };
            var result = await UserManager.CreateAsync(user, model.Password);
            if (result.Succeeded)
            {
                UserManager.AddToRole(user.Id, model.UserRole.ToString());

                return RedirectToAction("Index", "Home");                 
            }
            AddErrors(result);
        }

        // If we got this far, something failed, redisplay form
        return PartialView("~/Views/Account/Register.cshtml",model);
    }

EDIT 2:

When getting the roles like this:

var roles  = context.Roles.ToList();

I can see all the roles and I can also see which users have the specific role:

enter image description here

EDIT 3:

Tried turning lazyloading off and on

 this.Configuration.LazyLoadingEnabled = true;

 this.Configuration.LazyLoadingEnabled = false;

Still doesn't give me the roles data.

Upvotes: 7

Views: 3143

Answers (3)

yibe
yibe

Reputation: 378

You can use context.Roles instead of context.Users as follows and filter the target user roles. The trick is to filter the target user roles in the where method.

string Id="the_required_user_Id";

 var roles = context.Roles
                    .Include(r => r.Users)
                    .Where(r => (r.Users.Select(u => u.UserId).Contains(Id)))
                    .ToList();

This worked fine for me, hopefully this helps someone

Upvotes: 0

ThunD3eR
ThunD3eR

Reputation: 3456

So far I have not been able to solve this they way I want. I made a work arround that works:

I created a method that got me each individual user role like so:

    public string GetRole(string userId)
    {
        var role = UserManager.GetRoles(userId);

        return role[0];
    }

and in my original Getuser method i called the my recently developed method:

    public UserModel GetUsers()
    {
       var allUsers = context.Users.Include("Roles").ToList();

       var model = new UserModel
         {
            Users = allUsers.Select(x => new OverWatchUser
            {
                Email = x.Email,
                EmailConfirmed = x.EmailConfirmed,
                FirstName = x.FirstName,
                LastName = x.LastName,
                OrgId = x.OrgId,
                Role = GetRole(x.Id)

            }).ToList()

        };

        return model;
    }

This gives me the data I want but I consider this a dirty fix and I hope someone out there has a proper solution to the problem.

Upvotes: 1

TaiT&#39;s
TaiT&#39;s

Reputation: 3216

You have to load related entities you want to use with Include like this :

var allUsers = context.Users.Include(u => u.Roles).ToList();

Then you should be able to access user roles.

More info about that topic here

Upvotes: 5

Related Questions