Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 304
ThunD3eR Avatar asked Aug 24 '16 10:08

ThunD3eR


Video Answer


2 Answers

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

like image 101
TaiT's Avatar answered Oct 05 '22 06:10

TaiT's


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.

like image 32
ThunD3eR Avatar answered Oct 05 '22 06:10

ThunD3eR