简体   繁体   English

EF Code First强制急切加载

[英]EF Code First forced eager loading

I'm using EF 5 with Code First. 我正在使用EF 5和Code First。 I have a class that I want to always eager load some properties. 我有一个类,我想总是急于加载一些属性。 I removed the virtual keyword but it's not eager loading: 我删除了虚拟关键字,但它并不急于加载:

public class Person
{
   public ICollection<Email> Emails { get; set; } 
   public Profile Profile {get;set;}
}

So by turning off lazy loading, it won't auto eager load right? 所以通过关闭延迟加载,它不会自动加载正确的加载吗? If so, how do I archive that without using Include()? 如果是这样,如何在不使用Include()的情况下归档?

Thanks! 谢谢!

No, turning off lazy loading by removing the virtual keyword will not automatically enable eager loading. 不,通过删除virtual关键字来关闭延迟加载不会自动启用急切加载。 You have to Include the related Entity or Collection like so: 您必须Include相关的EntityCollection如下所示:

var personWithProfile = ctx.People.Include(x => x.Profile).First();
var personWithProfileAndEmails = ctx.People.
                                           .Include(x => x.Profile)
                                           .Include(x => x.Emails)
                                           .First();

This is a great read from the ADO.NET team blog: http://blogs.msdn.com/b/adonet/archive/2011/01/31/using-dbcontext-in-ef-feature-ctp5-part-6-loading-related-entities.aspx 这是来自ADO.NET团队博客的精彩内容: http//blogs.msdn.com/b/adonet/archive/2011/01/31/using-dbcontext-in-ef-feature-ctp5-part-6 -上下相关-entities.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM