Enable throwing when accessing non-included property when eager loading
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 25/100
Research direction
Start by reading ConfigureWarnings and CoreEventId.NavigationLazyLoading, then trace how eager-loading Include and ThenInclude behave with the SQL Server provider. Define the warning or throw behavior for accessing properties not included, using the contrasting examples in the issue to verify what completion should mean.
Written by the indexing model from the issue text.
Description
Is there a way to configure EF via ConfigureWarnings or some other mechanism to throw or show warnings when attempting to use a property that's not loaded via include call when using eager loading? Something like CoreEventId.NavigationLazyLoading but for Eager loaded properties?
Something like
var foo = dbContext.Foo.Include(x => x.Bar).First();
var prop = foo.NestedProperty;
// throws
var foo = dbContext.Foo.Include(x => x.Bar).Include(x => x.NestedProperty).First();
var prop = foo.NestedProperty;
// OK
Reasoning
I've inherited a pretty convoluted DB model that I don't control where having this would cut QA massively, basically some models have collections which may be replaced with collections from a related property if certain props are null, trivial example(usually we don't depend only on a single property):
var result = dbContext.Entities
.Include(x => x.RelatedEntity)
.ThenInclude(x => x.EntityCollection)
.Include(x => x.EntityCollection);
if(result.RelatedEntity != null) {
result.EntityCollection = result.RelatedEntity.EntityCollection
}
Due to the team size(and entity size) this has been causing issues long term and forcing us to add bunch of business logic and validation into the domain objects while it could be trivialized with Automapper or a similar library if we had the guarantee that the model was loaded correctly.
EF Core version:
Database provider: (Microsoft.EntityFrameworkCore.SqlServer)
Target framework: (.NET 5.0/.NET Core 3.1)
- Dominant language
- C#
- Stars
- 14.8k
- Forks
- 3.4k
- Avg merge
- 2d 5h
- Merged PRs (30d)
- 134
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from dotnet/efcore
-
Difficulty 4/5 3-5 days Newbie friendliness 55/100
-
customer-reported
Difficulty 5/5 Over a week Newbie friendliness 38/100
-
area-cosmos area-vector-search
Difficulty 5/5 Over a week Newbie friendliness 25/100
-
area-cosmos
Difficulty 5/5 Over a week Newbie friendliness 25/100
-
area-tools needs-design
Difficulty 4/5 3-5 days Newbie friendliness 25/100
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
-
:watch: Not Triaged 11.0 fundamentals/subsvc
Difficulty 2/5 1-3 hours Newbie friendliness 92/100
dotnet/AspNetCore.Docs#37699 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
SubtitleEdit/subtitleedit#15108 · 1 comment ·
-
area/docs-content Bug pulumi/docs
Difficulty 1/5 1-3 hours Newbie friendliness 94/100
-
agentic-workflows untriaged
Difficulty 2/5 1-3 hours Newbie friendliness 76/100