简体   繁体   English

EF4中未填充外键对象

[英]Foreign key object not getting filled in EF4

I would think this is very basic stuff, but I'm just not getting it to work. 我认为这是非常基本的东西,但我只是没有使它起作用。

I try to get a list of objects using lambda expressions like this : 我尝试使用像这样的lambda表达式获取对象列表:

 List<LocalizationGlobalText> list = _entities.LocalizationGlobalTexts.Where(l => l.Language.Id == _currentlanguage).ToList<LocalizationGlobalText>();

The list is fetched, but the foreign key objects are all null. 已获取列表,但外键对象均为空。 I also tried using LINQ to entities but this results in the same problem : 我也尝试对实体使用LINQ,但这会导致相同的问题:

        IEnumerable<LocalizationGlobalText> bla = (from lgt in _entities.LocalizationGlobalTexts
                                                   join lg in _entities.LocalizationGlobals on lgt.IdLocalizationGlobal equals lg.Id
                                                   where lgt.IdLanguage == _currentlanguage
                                                   select lgt);

By default, Entity Framework only brings in the collection that you specify, without any foreign objects. 默认情况下,实体框架仅引入您指定的集合,而没有任何异物。 If you have lazy loading enabled, accessing the foreign properties will cause them to be lazily initialized. 如果启用了延迟加载,则访问外部属性将导致延迟初始化它们。 If not, you'll need to tell entity framework to eagerly load the properties you want with the first batch. 如果不是,则需要告诉实体框架热切地在第一批中加载所需的属性。

There are two ways to do this. 有两种方法可以做到这一点。 The first is the "official" way, but I don't like it because it uses magic strings: 第一种是“官方”方式,但我不喜欢它,因为它使用了魔术字符串:

var list = _entities.LocalizationGlobalTexts.Include("ForeignProp")
              .Where(l => l.Language.Id == _currentlanguage)
              .ToList<LocalizationGlobalText>();

(Replace "ForeignProp" with the name of the property you want it to eagerly load) (将“ ForeignProp”替换为您希望其快速加载的属性的名称)

The second way is to set up your selector so that it will be forced to pull this extra data in: 第二种方法是设置选择器,以便将其强制拉入以下额外数据:

var list = _entities.LocalizationGlobalTexts
              .Where(l => l.Language.Id == _currentlanguage)
              .Select(l => new {l, l.ForeignProp})
              .ToList();

foreach(var item in list)
{
    Console.WriteLine(item.l.Name + item.ForeignProp.Title);
}

Since Entity Framework is smart enough to have made the appropriate connections, you could throw on one more selector and avoid using the anonymous type afterward: 由于Entity Framework足够聪明,可以进行适当的连接,因此您可以抛出另一个选择器,避免以后再使用匿名类型:

var list = _entities.LocalizationGlobalTexts
              .Where(l => l.Language.Id == _currentlanguage)
              .Select(l => new {l, l.ForeignProp})
              .AsEnumerable() // tells EF to load now. The rest is LINQ to Objects
              .Select(i => i.l)
              .ToList();

foreach(var localization in list)
{
    Console.WriteLine(localization.Name + localization.ForeignProp.Title);
}

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

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