locked
Include In UserManager Asp.Net Core RRS feed

  • Question

  • User338455301 posted

    Hi,
    my Asp.NetUsers table is related with Customer table and other tables (one to one)

    i want use this code _userManager.GetUsersInRoleAsync("Customer")  to get customer users but i want set include to customer table.

    how can i use include in _userManager.GetUsersInRoleAsync to get users and include with other tables     

    Saturday, April 18, 2020 6:15 AM

All replies

  • User475983607 posted

    The community cannot see your table schema and therefore cannot answer this question.  However you can write a standard LINQ query to include a table.

    var results = from c in _context.Customers.AsNoTracking()
      .Include(u => u.User).AsNoTracking()
      .Select c;

    Saturday, April 18, 2020 10:55 AM
  • User338455301 posted

    i want get users is role customer 
    i want get users by role with include by _userManger

    Saturday, April 18, 2020 11:15 AM
  • User475983607 posted

    elahi1mahdi

    i want get users is role customer 
    i want get users by role with include by _userManger

    Join the roles!

    var results = from c in _context.Customer.AsNoTracking()
    			  .Include(u => u.User).AsNoTracking()
    				join ur in _context.UserRoles.AsNoTracking() on c.User.Id equals ur.UserId
    				join r in _context.Roles.AsNoTracking() on ur.RoleId equals r.Id
    			  where ur.RoleId >= roleId
    			  select c

    Keep in mind, if you go through the userManager, you have to make at least two queries.   You can learn LINQ at the following link.

    https://docs.microsoft.com/en-us/dotnet/csharp/linq/perform-inner-joins

    Saturday, April 18, 2020 11:35 AM
  • User711641945 posted

    Hi elahimahdi,

    As mgebhard said,if you use userManager,you need at least two queries.

    Model:

    public class ApplicationUser : IdentityUser
    {
        public Customer Customer { get; set; }
    }
    public class Customer
    {
        public int Id { get; set; }
        public string CustomName { get; set; }
    }

    Controller:

    var user = new List<ApplicationUser>() { };
    var data =_userManager.GetUsersInRoleAsync("Customer").Result;
             
    foreach (var item in data)
    {
        var user1 = _userManager.Users.Include(a => a.Customer).FirstOrDefault(u => u.Id == item.Id);
        user.Add(user1);
    };

    Best Regards,

    Rena

    Monday, April 20, 2020 9:09 AM
  • User338455301 posted

    thanks i use this way now ,

     but as you said we need at least two queries. its bad , because i have many users in my database  and i want get users in many actions .

    there is no other way to use one query to this work ?  

    Wednesday, April 22, 2020 6:54 AM
  • User475983607 posted

    but as you said we need at least two queries. its bad , because i have many users in my database  and i want get users in many actions .

    there is no other way to use one query to this work ?  

    I shared a working example above.  Although my table is named UserProfile not customer.  Not sure what else I can do for you...

    Wednesday, April 22, 2020 9:28 AM