实体框架多对多查询:未生成导航属性

发布于 2024-11-04 01:36:50 字数 1460 浏览 0 评论 0 原文

我有两个表 UserUserRole,它们使用链接表 UserInRole 连接

当我生成实体模型时,由于某种原因,实体UserInRole 未生成。从图中可以看出,实体框架理解 UserUserRole 之间存在多对多关系:

在此处输入图像描述

我需要像这样实现查询

select ur.Name from [User] as u
inner join UserInRole uir on uir.UserId = u.Id
inner join UserRole ur on ur.Id = uir.UserRoleId
where u.Username = 'magename'

我正在使用通用存储库,如果导航属性存在,查询将如下所示:

from u in repository.AsQueryable<User>()
join uir in repository.AsQueryable<UserInRole>() on u.Id equals uir.UserId
join ur in repository.AsQueryable<UserRole>() on uir.UserId equals ur.Id
where u.Username == userName
select ur.Name

但实体框架不存在产生UserInRoles 导航属性和相关的 UserInRole 实体,所以问题是在这种情况下我应该做什么?我是否应该删除 UserInRoleUserRole 之间的链接以获取模型中生成的实体 UserInRole 或者有什么方法可以让我上面描述的查询不进行任何更改在数据库中?

已更新

,看起来我需要做类似的事情,

stirng[] roles = (from u in repository.AsQueryable<User>()
         where u.Username == userName
         select ur.UserRoles.Select(x => x.Name)).ToArray<string>();

收到错误 Cannot 隐式转换类型 'System.Collections.Generic.List>'到 'string[]' 有什么想法吗?

I have two tables User and UserRole which are they connected using a link table UserInRole

When I generate the entity model, for some reason, the entity UserInRole not getting generated. And as you can see from the picture, Entity Framework understood that there is many-to-many relationship between User and UserRole:

enter image description here

I need to implement the query like this

select ur.Name from [User] as u
inner join UserInRole uir on uir.UserId = u.Id
inner join UserRole ur on ur.Id = uir.UserRoleId
where u.Username = 'magename'

I am using generic repository, if navigation property would exists the query would be looking like this:

from u in repository.AsQueryable<User>()
join uir in repository.AsQueryable<UserInRole>() on u.Id equals uir.UserId
join ur in repository.AsQueryable<UserRole>() on uir.UserId equals ur.Id
where u.Username == userName
select ur.Name

But Entity Framework does not generate UserInRoles navigation property and related UserInRole entity, so the question what should I do in that situation? Should I remove link between UserInRole and UserRole to get entity UserInRole generated in model or there any way to have the query I described above without any changes in database?

UPDATED

so looks i need to do something like that

stirng[] roles = (from u in repository.AsQueryable<User>()
         where u.Username == userName
         select ur.UserRoles.Select(x => x.Name)).ToArray<string>();

getting an error Cannot implicitly convert type 'System.Collections.Generic.List<System.Collections.Generic.IEnumerable<string>>' to 'string[]' any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

舞袖。长 2024-11-11 01:36:50

您的 EF 模型中不需要该链接表!这就是EF的魅力!!

检查您的实体 User - 它有一个 UserRoles 导航属性 - 这是该用户所属的所有角色的集合。

检查实体 UserRole:它有一个导航属性Users,其中包含具有此角色的所有用户。

通过这两个多值导航属性,您可以表达您可能想要的任何查询:

  • 查找给定角色的所有用户(查找 UserRole 实体并枚举其 .Users 属性)
  • 查找给定用户的所有角色(找到 User 并枚举其 .UserRoles

EF 和 EDM 对您“隐藏”该链接表 - 您当你在概念模型中表达你的意图时,并不真正需要它;这些链接表只是关系数据库中的“必要之恶”,因为这些数据库无法以任何其他方式建模 m:n 关系。

更新:所以你似乎想要找到特定用户所在的所有用户角色(他们的名字)的列表 - 你可以这样表达:

// find the user in your "dbContext" (or via your repository)
var user = _dbContext.Users.FirstOrDefault(u => u.Name == "magename");

// from the list of UserRoles on that user, select the names only
var userRolesNames = user.UserRoles.Select(ur => ur.Name).ToList();

You don't need that link table in your EF model! That's the beauty of EF!!

Check your entity User - it has a UserRoles navigation property - that's the collection of all roles this user is in.

Check out entity UserRole: it has a navigation property Users which has all users that are in this role.

With these two multi-valued navigation properties, you can express any of the queries you might want:

  • find all users for a given role (find the UserRole entity and enumerate its .Users property)
  • find all roles for a given user (find the User and enumerate its .UserRoles)

EF and the EDM sort of "hide" that link table from you - you don't really need it when you're expressing your intent in the conceptual model; those link tables are just a "necessary evil" in relational databases since those databases cannot model m:n relationships in any other way.

Update: so you seem to want to find a list of all the user roles (their names) that a particular user is in - you can express this something like this:

// find the user in your "dbContext" (or via your repository)
var user = _dbContext.Users.FirstOrDefault(u => u.Name == "magename");

// from the list of UserRoles on that user, select the names only
var userRolesNames = user.UserRoles.Select(ur => ur.Name).ToList();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文