我有两个表 User
和 UserRole
,它们使用链接表 UserInRole
连接
当我生成实体模型时,由于某种原因,实体UserInRole
未生成。从图中可以看出,实体框架理解 User
和 UserRole
之间存在多对多关系:
我需要像这样实现查询
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
实体,所以问题是在这种情况下我应该做什么?我是否应该删除 UserInRole
和 UserRole
之间的链接以获取模型中生成的实体 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
:
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?
发布评论
评论(1)
您的 EF 模型中不需要该链接表!这就是EF的魅力!!
检查您的实体
User
- 它有一个UserRoles
导航属性 - 这是该用户所属的所有角色的集合。检查实体
UserRole
:它有一个导航属性Users
,其中包含具有此角色的所有用户。通过这两个多值导航属性,您可以表达您可能想要的任何查询:
UserRole
实体并枚举其.Users
属性)User
并枚举其.UserRoles
)EF 和 EDM 对您“隐藏”该链接表 - 您当你在概念模型中表达你的意图时,并不真正需要它;这些链接表只是关系数据库中的“必要之恶”,因为这些数据库无法以任何其他方式建模 m:n 关系。
更新:所以你似乎想要找到特定用户所在的所有用户角色(他们的名字)的列表 - 你可以这样表达:
You don't need that link table in your EF model! That's the beauty of EF!!
Check your entity
User
- it has aUserRoles
navigation property - that's the collection of all roles this user is in.Check out entity
UserRole
: it has a navigation propertyUsers
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:
UserRole
entity and enumerate its.Users
property)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: