Like a simple UserProfiles to Roles membership in a UserInRoles table
I created a relationship between UserProfiles and Clients in the UserInClients table using this code
modelBuilder.Entity<UserProfiles>() .HasMany<dbClient>(r => r.Clients) .WithMany(u => u.UserProfiles) .Map(m => { m.ToTable("webpages_UsersInClients"); m.MapLeftKey("ClientId"); m.MapRightKey("UserId"); });
My UserProfiles has public virtual ICollection<dbClient> Clients { get; set; } public virtual ICollection<dbClient> Clients { get; set; } public virtual ICollection<dbClient> Clients { get; set; } , and my Clients has public virtual ICollection<UserProfiles> UserProfiles { get; set; } public virtual ICollection<UserProfiles> UserProfiles { get; set; }
- If you need to see how the models tell me, I can publish them
In the model and representation I would like
- Display all clients (Distinct) and show how many users have access to this client
- Create a view that displays only clients who are allowed to view the current user.
I thought it was as simple as accessing the properties of my Clients.ClientID models, I tried things like Clients.ClientID.select (u => u.UserId == clientid), but I knew better and I know that it does not and will not work.
My other thoughts were to create a model with CliendID and UserID in it (for example, in the created table), so I can use the connection in my controller to find the correct values
In the end, I'm trying to include this line in GetCascadeClients JsonResult
return Json(db.Clients.Select(c => new { ClientID = c.ClientID, ClientName = c.Client }), JsonRequestBehavior.AllowGet);
My question is when I'm in my controller, how do I access this table created by Entity Framework?
EDIT:
SOLUTION OF THE PROBLEM. Gather both answers together.
return Json(db.Clients .Include(c => c.UserProfiles) .Where(c => c.UserProfiles.`Any(up => up.UserName == User.Identity.Name))` .Select(c => new { ClientID = c.ClientID, ClientName = c.Client, UserCount = c.UserProfiles.Count() }), JsonRequestBehavior.AllowGet);