[英]Related entities not loading when using Include with Linq to Entities
In my schema, I have the following relevant entities: 在我的架构中,我具有以下相关实体:
Basic schema for Element/Folder/User/Role 元素/文件夹/用户/角色的基本架构
I'm trying to load a list of the Folders that each User has access to - either directly or by virtue of the User having a Role that has access to the Folder. 我正在尝试加载每个用户有权访问的文件夹的列表-直接或借助于具有角色访问该文件夹的角色的用户。 I've split the requirement into two pieces (direct and indirect) and used a Union to pull the records back in a single collection, with only one call to the database. 我已将需求分为两部分(直接和间接),并使用Union将记录拉回到单个集合中,而只调用了一个数据库。
I've got the L2E statement below running - as long as a User has a (direct or indirect) record in Access, the Folder entity will be returned in the list (Note: sUserElmKey is the key of the current User). 我在下面运行了L2E语句-只要用户在Access中具有(直接或间接)记录,文件夹实体就会在列表中返回(注意:sUserElmKey是当前用户的键)。
Dim fldList = (From fld As Folder In ctxClient.Elements.OfType(Of Folder)() _
Join acs As Access In ctxClient.Accesses
On acs.ElementKey Equals fld.ElementKey
Where acs.UserRoleElementKey = sUserElmKey _
Select fld
).Union _
(From fld As Folder In ctxClient.Elements.OfType(Of Folder)() _
Join acs As Access In ctxClient.Accesses
On acs.ElementKey Equals fld.ElementKey
Join rol As Role In ctxClient.Elements.OfType(Of Role)()
On rol.ElementKey Equals acs.UserRoleElementKey
Where (From usr As User In rol.Users
Where usr.ElementKey = sUserElmKey).Any _
Select fld
)
This worked fine when I just needed to know if any access to each Folder was available to the User. 当我只需要知道用户是否可以访问每个文件夹时,这种方法就可以正常工作。 Now I need to define other logic based on which AccessTypeCode the User has for each Folder. 现在,我需要根据用户对每个文件夹使用的AccessTypeCode定义其他逻辑。
Given that I need to iterate over the resulting entities in the collection in order to load a different collection of objects that represents nodes in the tree UI, I figured I'd just add an Include("Accesses") to each sub-statement above. 鉴于我需要遍历集合中的结果实体以加载代表树UI中节点的对象的不同集合,因此我认为我只需在上面的每个子语句中添加一个Include(“ Accesses”) 。 As I iterated through the resulting collection, I could write the logic to set the needed flags for each of the new node objects (eg if the User has AccessTypeCode "VIEW", then they have read-only access; "OWN" or "NEW" and they have read/write access). 当我遍历结果集合时,我可以编写逻辑来为每个新节点对象设置所需的标志(例如,如果用户具有AccessTypeCode“ VIEW”,则它们具有只读访问权限;“ OWN”或“ NEW” ”,并且他们具有读/写访问权限)。
Unfortunately, I haven't been able to find where to add the Include to do this properly - I've put it after the "ctxClient.Elements" as well as after the ".OfType(Of Folder()", but while the code compiles and runs, the debugger shows 0 records for the Accesses collection under each Folder when I'm iterating. 不幸的是,我无法找到将Include添加到何处以正确执行此操作-我将其放在“ ctxClient.Elements”之后以及“ .OfType(Of Folder()”之后),但是代码编译并运行时,调试器在进行迭代时在每个Folder下显示Accesses集合的0条记录。
Rewriting this as a lambda expression is an option, but I think I might just find myself in the same place. 将其重写为lambda表达式是一种选择,但我想我可能会发现自己在同一个地方。
Any ideas on how to resolve this? 有关如何解决此问题的任何想法?
Looking at you schema I think you can rewrite the query as follows: 查看您的架构,我认为您可以按以下方式重写查询:
(From fld In ctxClient.Elements.OfType(Of Folder)()
Where fld.Accesses.Any(Function(acs) acs.UserRoleElementKey = sUserElmKey)
Select fld)
.Union
(From fld In ctxClient.Elements.OfType(Of Folder)()
Where fld.Accesses.Any(Function(acs) acs.UserRoleElement.Users
.Any(Function(usr) usr.ElementKey = sUserElmKey))
Select fld)
This checks whether there is any access, directly or through roles, without having to Include
accesses. 这将检查是否直接或通过角色进行任何访问,而不必Include
访问。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.