问题
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