实体框架 - 加载相关实体 [英] Entity Framework - Eager loading related entities
问题描述
ObjectContext实例已被处理,不能再用于需要连接的操作。
从上下文中直接选择时,我可以使用 Include(string)
方法强制将其包含在生成的SQL查询中: / p>
var sellerers = context.Sellers.Include(recommendations.User)ToList();
但是,如果我有(例如)一个接受一个实体并需要所有项目的辅助方法被加载,没有包含
方法可用。
void Test卖家)
{
//确保所有建议及其用户加载
}
蛮力的方法是循环通过它们:
foreach(在卖家推荐的推荐)
recommendations.User.ToString(); //只是强制加载
如果我有100个建议,这将在后面创建101个SQL查询-scenes。理想情况下,我想要一种方法/方法,只需单独访问SQL即可加载所有建议
和用户
对象。 p>
显示我的钱。
编辑我不太在意讨论是否这是一个好或坏的建筑。为了这个问题我简化了我的场景。你可以用EF API做什么吗?
编辑2
Ladislav的编辑提供了一种新的方法的希望,但是似乎我不在那里。
我可以通过以下方式实现我想要的:
context.Sellers.Include(recommendations.User)。Single(s => s.Id == seller.Id);
此方法无法使用 LoadProperty
...
context.LoadProperty(seller,recommendations.User);
...因为失败,错误...
无法找到指定的导航属性Recommendation.User。
如果你没有上下文的引用,这些方法是有效的。
这是一个旧问题,但是在EF6中可以在此上的实体上实现加载依赖对象:
context.Entry(seller).Collection(s => s.Recommendations).Query()。Include(r => r.User)) 。加载();
这将加载所有建议
及其相关用户
针对给定的卖家
I'm using Entity Framework 4 with MVC and need to ensure any referenced entities I want to use in my view have been loaded before the controller method returns, otherwise the view spits out the dreaded:
The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.
When selecting straight from the context, I can just use the Include(string)
method to force them to be included in the generated SQL query:
var sellers = context.Sellers.Include("Recommendations.User").ToList();
However, if I have (for example) a helper method that accepts an entity and needs all items to be loaded, there's no Include
method available.
void Test(Seller seller)
{
// ensure all recommendations and their users are loaded
}
The brute force approach is to loop through them:
foreach (var recommendation in seller.Recommendations)
recommendation.User.ToString(); // just force a load
If I have 100 recommendations, this will create 101 SQL queries behind-the-scenes. Ideally I want a method/approach that loads all Recommendation
AND User
objects with only a single trip to SQL.
Show me the money.
EDIT I'm not really interested in discussing whether this is a good or bad architecture. I've simplified my scenario for the sake of the question. Can you do what I'm asking with the EF API?
EDIT 2
Ladislav's edit offered hope of a new approach, but it seems I'm not quite there.
I can achieve what I want via this:
context.Sellers.Include("Recommendations.User").Single(s => s.Id == seller.Id);
This approach doesn't work using LoadProperty
...
context.LoadProperty(seller, "Recommendations.User");
...as it fails with the error...
The specified navigation property Recommendations.User could not be found.
Neither of these approaches work if you don't have a reference to the context.
This is an old question, but in EF6 you can accomplish loading dependent objects on an entity like this:
context.Entry(seller).Collection(s => s.Recommendations).Query().Include(r => r.User)).Load();
That would load all Recommendations
and their related Users
for the given seller
这篇关于实体框架 - 加载相关实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!