如何加载相关的实体集合及其自己的相关实体? [英] How do I load a related Entity Collection along with its own related Entity?
问题描述
你好我有三个数据库表:
用户(UserID,FirstName,LastName)
客户端(ClientID,Name)
用户客户端(UserClientID) ,UserID,Name)
如果我在上下文中有用户,如何检索其相关的UserClient记录及其相关的客户记录?
我可以轻松加载UserClient记录如下:
CurrentUser.UserClients.Load();
然后逐个循环它们以加载它们相关的客户记录,但这是很多前往数据库。
我希望能够一次性加载UserClients和客户。
提前谢谢你,
克雷格
Howdy
I have three database tables:
Users (UserID, FirstName, LastName)
Clients (ClientID, Name)
UserClients (UserClientID, UserID, Name)
If I have a User in context, how do I retrieve its related UserClient records as well as their related Client record?
I can easily load the UserClient records like so:
CurrentUser.UserClients.Load();
and then cycle through them one at a time to load their related Client record, but that's a lot of trips to the database.
I'd like to be able to load both UserClients and Clients in one go.
Thankyou in Advance,
Craig
推荐答案
您好,您可以使用Include方法来预先加载实体。
您可以在这里阅读更多相关信息:
http://msdn.microsoft.com/en-us/library/bb896272.aspx
http://weblogs.asp.net/zeeshanhirani/archive/2008/08/14/eager-loading-entities-in- entity-framework.aspx
我希望它会对你有帮助。
Hi,
You can use the Include method to eager load entities.
You can read more about it here:
http://msdn.microsoft.com/en-us/library/bb896272.aspx
http://weblogs.asp.net/zeeshanhirani/archive/2008/08/14/eager-loading-entities-in-entity-framework.aspx
I hope it will help you.
这篇关于如何加载相关的实体集合及其自己的相关实体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!