I'm very new to Entity Framework Core, so I'm concerned about whether I've done this query in the most efficient way possible.
The database I'm querying looks like this:
Basically, UserRoles
is a a "linking table" between the User
and the Roles
in order to create a one-to-many relationship. If a user exists at all, they will always have at least one role, but they could have several roles.
I want to get the role name based on the NT ID of the user.
This is something I plan to do fairly often, so I separated it out into a separate utility (rather than having it in the controller).
In SQL, I'd just do two inner join
s. However, what I did here is to query UserRoles
(rather than using Join
).
public class UserUtilities
{
public static List<Roles> GetRoles(string ntid)
{
var ctx = new MyLiveDocsContext();
List<Roles> roles =
(from ur in ctx.UserRoles
where ur.User.Ntid == ntid
select ur.Role).ToList();
return roles;
}
}
Ive tested this and it does what I want, but I'm wondering if this is the best way to do this. For example, would it be better to use an explicit join
instead?
using
if you want all the resources that the context controls to be disposed at the end of the block." \$\endgroup\$ – BCdotWEB Mar 5 '20 at 12:07