On this page of the Entity Framework Core documentation, it says when querying loaded data:
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.
This is true whether it is Eager or Explicit.
I find this to be frustrating, because it will return partial data, which makes it seem like a complete list because there's nothing indicating that it is partial.
Example:
Say I have the following two classes:
class User {
int Id { get; set; }
string Name { get; set; }
List<Message> Messages { get; set; }
}
class Message {
int Id { get; set; }
List<User> Users{ get; set; }
}
And I query using the following code:
_dbContext.Users
.Include(u => u.Messages)
.Single(u => u.Id == 1);
My output is the following:
"user" {
"id": 1,
"name": "Alice",
"messages": [
{
"id": 1,
"users": [
{
"id": 1,
"name": "Alice",
}
]
}
]
}
I would expect that unless I also added .ThenInclude(m => m.Users)
, it would give me a null or empty list, not a partial list.