2016-07-28 102 views
1

我一直在这个几个小时,所以我不知道如果我太累了,或者我只是失去了一些东西...EF7。包括问题

我想要使用EF7.0加载此图的整个路径,并且我没有通过编译器!请问有人给我的语法糖来包括这个路径?

enter image description here

给定一个UserTopic - 我需要所有相关的实体加载到矢量!

回答

1

好吧在我的例子和问题(我可能会一直措辞更好)。我需要返回一个UserTopic,并显示加载的对象图。所以,关我去我的快乐的同时,我在EF6已经做过很多次..

UserTopic queryUT = dbContext.UserTopics.FirstorDefault(ut => ut.UserTopicID = 1).Include(... #FAIL 

OK ..另一种尝试

var queryUT = dbContext.UserTopics.Where(ut => ut.UserTopicID = 1).Include ... #FAIL (start include with a collection 

这样的例子不胜枚举... ...分辨率

public void ProcessUserTopic(dbContext pdbContext, UserTopic pUserTopic) 
var qeuryUT= pdbContext.UserTopics 
              .Include(lpUserTopic => lpUserTopic.UserTopicInteractions) 
              .ThenInclude(lpUserTopInteraction => lpUserTopInteraction.Interaction) 
              .ThenInclude(lpInteraction => lpInteraction.InteractionProfile) 
              .ThenInclude(lpProfile => lpProfile.ProfileVectors) 
              .ThenInclude(lpProfileVector => lpProfileVector.Vector) 
              .FirstOrDefault(lpUserTopic => lpUserTopic.UserTopicID == pUserTopic.UserTopicID) 

请注意,我无法遍历对象图,只需传入userTopic,然后遍历图即可。我必须回到dbContext,包含对象图FIRST,然后过滤已通过的用户主题。

过去,我隔离了我先要的东西,然后遍历了对象图(所以我依靠延迟加载)。如果我想加载,我在过滤器之后。

在EF7中,您必须总是遍历FIRST对象树(使用多个.Include /。然后包含语句),然后进行过滤。另外,一旦在一个实体上被隔离,你不能展开对象图 - 因为它只会返回空值,即使DB有值(我知道,惰性加载不在EF7中)。然而,我想我可以遍历图形,在隔离后只有一个实体(我敢肯定是EF6的一个变化)。

+0

很高兴听到问题解决+1 –

2

您的链接包括:

dbContext.UserTopics.Include(x => x.UserTopicProfile).ThenInclude(...)....ThenInclude(x => x.Vector); 
+0

这不是我正在寻找的完整答案,但它让我开始了正确的道路,就像你从上下文开始的那样! – codeputer