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());
}
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)?
Here's a screenshot:
My model is defined as so:
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();
}
}