Loading relations in linq2entities automatically

半腔热情 提交于 2019-12-13 20:20:38

问题


When i have a relation between two entities in my model:

[GroupMember] (*) ----- (1) [User]

and tries to select items from this relation with LINQ:

From entity in _user.GroupMember select entity

I always get an empty result unless I load the relation first with following statement:

_user.GroupMember.Load()

Is there a way to avoid loading the relations like this?


回答1:


If you have cascading relations, you can handle them with .Include("GroupMember.AnotherTable.YetAnotherTable") which is a little nicer than having to do chained Include calls.




回答2:


I just realized that when i load the User from the database, I can use Include to load GroupMember with the User like this:

Users=from entity in db.User.Include("GroupMember") select entity

But if I have several relations and maybe wants to access relations on the relations, this gets very ugly.

So I am still looking for a better/nicer solution to my issue.



来源:https://stackoverflow.com/questions/585127/loading-relations-in-linq2entities-automatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!