实体框架急于加载单个实体 [英] Entity Framework is eager loading a single entity

查看:76
本文介绍了实体框架急于加载单个实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的WebApi控制器的简化版本如下:

The abridged version of my WebApi controller is like so:

[HttpGet, Route("")]
public async Task<IHttpActionResult> Search(bool includeEntities)
{
    IQueryable<VersionTopic> results = DbContext.VersionTopics;
    if (includeEntities)
    {
        results = results.Include(o => o.CreatedBy);
        results = results.Include(o => o.LastSavedBy);
        results = results.Include(o => o.Topic.LastSavedBy);
        results = results.Include(o => o.Topic.CreatedBy);
        results = results.Include(o => o.Topic.PACKType.LastSavedBy);
        // etc...
    }

    results = results.OrderBy(o => o.SortOrder);

    return Ok(result.ToList());
}

由于某种原因, LastSavedBy includeEntities 参数为false,也总是填充$ c>实体。

For some reason, the LastSavedBy entity is ALWAYS populated, even when the includeEntities parameter is false.

为什么会急于加载该实体,而又不加载任何其他实体(按要求)?

Why would it be eager loading just this one entity but none of the others (as is required)?

以下是屏幕截图:

我的模型是定义如下:

public class VersionTopic
{
    [Key]
    [Required]
    public Guid VersionTopicId { get; set; }

    [Required]
    [Index("IX_VersionTopic_VersionId_TopicId", IsUnique = true, Order = 0)]
    public Guid VersionId { get; set; }

    [Required]
    [Index("IX_VersionTopic_VersionId_TopicId", IsUnique = true, Order = 1)]
    public Guid TopicId { get; set; }

    [Required(AllowEmptyStrings = true)]
    [MaxLength(250)]
    public string Name { get; set; }

    public string KeyMessage { get; set; }

    [Required]
    public int SortOrder { get; set; }

    [Required]
    public Guid CreatedById { get; set; }

    [Required]
    public DateTime CreatedDate { get; set; }

    [Required]
    public Guid LastSavedById { get; set; }

    [Required]
    public DateTime LastSavedDate { get; set; }

    public virtual ICollection<VersionRecommendation> VersionRecommendations { get; set; } = new List<VersionRecommendation>();

    [ForeignKey("CreatedById")]
    public virtual ApplicationUser CreatedBy { get; set; }

    [ForeignKey("LastSavedById")]
    public virtual ApplicationUser LastSavedBy { get; set; }

    [ForeignKey("TopicId")]
    public virtual Topic Topic { get; set; }

    [ForeignKey("VersionId")]
    public virtual Version Version { get; set; }

    public VersionTopic()
    {
        VersionTopicId = Guid.NewGuid();
    }
}


推荐答案

加载相关数据-急需加载部分,但适用于EF6及以下版本:

It's basically explained in the following Tip from Loading Related Data - Eager Loading section of the EF Core documentation, but the same applies to EF6 and below:


实体框架Core会将导航属性自动修复为先前已加载到上下文实例中的任何其他实体。因此,即使您未明确包含导航属性的数据,如果先前已加载某些或所有相关实体,也可能仍会填充该属性。

Entity Framework Core will automatically fix-up navigation properties to any other entities that were previously loaded into the context instance. So even if you don't explicitly include the data for a navigation property, the property may still be populated if some or all of the related entities were previously loaded.

不幸的是,这种行为是不可控制的,因此要避免它的唯一选择是使用全新的 DbContext (或手动清理导航属性不需要,但这很烦人,很容易忘记)。

Unfortunately this behavior is not controllable, so the only options to avoid it is to use fresh new DbContext (or manually cleaning up the navigation properties which are not needed, but that's annoying and easy to forget).

这篇关于实体框架急于加载单个实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆