EF Core materializes projection property as object with default values
See original GitHub issueI have a LINQ expression like the following:
from u in Context.Users.Where(u => u.Id == 123).Select(UserDto.Map)
join l in Context.Locations.Select(LocationDto.Map) on u.LocationId equals l.Id into locations
from l in locations.DefaultIfEmpty()
join a in Context.Agencies.Select(AgencyDto.Map) on l.CompanyId equals a.Id into agencies
from a in agencies.DefaultIfEmpty()
join m in Context.Markets.Select(MarketDto.Map) on l.CompanyId equals m.Id into markets
from m in markets.DefaultIfEmpty()
select new UserEntityData {
User = u,
Location = l,
Agency = a,
Market = m
}
User via Location can be connected to Agency or Market, not both. EF translates it correctly into SQL query with left joins. Query returns correct data with Agency or Market columns all NULL. However when query data is materialized I get UserEntityData objects with both Agency and Market properties not null. UserEntityData property for missing DB record is initialized with object with default values. I expect property to be null.
May be usefull:
- If I read full entities for agency\market (not projected DTOs) all works fine.
- I tried EF Core 5\6 and got another error ‘Nullable object must have a value’ during materialization. Looks like EF tries to put NULLs from DB response into not-nullable properties of my DTOs.
Project to reproduce the original issue: EfTest.zip
EF Core version: 3.1.15 Database provider: Microsoft.EntityFrameworkCore.SqlServer Target framework: .Net Framework 4.8
Issue Analytics
- State:
- Created 2 years ago
- Reactions:12
- Comments:9 (5 by maintainers)
Top Results From Across the Web
Generated Values - EF Core
This page details various patterns for configuration value generation with EF Core.
Read more >Tracking vs. No-Tracking Queries - EF Core
When materializing an entity, EF Core returns the same entity instance from the change tracker if it's already being tracked. If the result ......
Read more >Nullable entity projection in Entity Framework
Here lies the problem - as soon as Entity Framework encounters a message with UserID == NULL, it throws an exception, saying that...
Read more >Entity Framework Core 2.1: Heck Yes, It's Production Ready!
Now EF Core can materialize results if the only available constructors have parameters. If there are multiple constructors, EF Core uses the ...
Read more >EF core best practices : r/csharp
Try using projections are returning only the data that is actually needed. You do projections by using Select. Say you have a table...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
It is same. Refer to this comment https://github.com/dotnet/efcore/issues/22517#issuecomment-692236476
@smitpatel are you sure it’s the same thing? From https://github.com/dotnet/efcore/issues/22517: “confirmed this works in 3.1, but fails in latest daily” - my example fails in 3.1 also.