考虑以下实体模型和功能:
public class Order
{
public int OrderId {get; set;}
public int StatusId {get; set;}
public virtual Status OrderStatus {get; set;}
}
public class Status
{
public int StatusId {get; set;}
public String Name { get; set;}
}
public void ShowOrders()
{
//load all status entities.
//Will EF check for these in object cache first when I access order.Status for
//the first time?
//Or perhaps even auto include them in materialised orders?
context.Status.Load();
//enumerate orders without explicit status include
foreach(Order o in context.Orders.ToList())
{
//Get Status navigation property for each order
//Will database be hit?
Console.WriteLine("Order: {0:N}, Status: {1}", o.OrderId, o.OrderStatus.Name);
}
}
我知道我可以明确地做到:
context.Orders.Include(o=>o.OrderStatus).ToList();
在查询订单时包括状态以防止 n+1 选择。而且我知道,如果我访问已检查的Order.OrderStatus
导航属性DbReferenceEntry.IsLoaded
并在数据库被命中之前检索缓存的状态对象(如果可能)。
我想知道的是,如果引用实体已经在对象缓存中,那么当父实体实现(即使没有被调用)时是否填充DbReferenceEntry.IsLoaded
?DbReferenceEntry.CurrentValue
.Include()
所以在上面的这个例子中,当Order.OrderStatus
第一次访问时,即使所有状态都在对象缓存中,因为Status.Load()
枚举订单之前的调用,数据库查询是否会被执行?