question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

EF Core materializes projection property as object with default values

See original GitHub issue

I 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:

  1. If I read full entities for agency\market (not projected DTOs) all works fine.
  2. 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:closed
  • Created 2 years ago
  • Reactions:12
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
smitpatelcommented, Jan 18, 2022
0reactions
Alexander-Leontievcommented, Jan 17, 2022

@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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found