I use Entity Framework Core 7. When I declare an entity that is guaranteed to have a reference property, I declare it as non-nullable:
public class Employee {
public Employer Employer { get; set; } // no such thing as employee without an employer.
}
However, Employer
property is not guaranteed to be loaded by Entity Framework (I don't use lazy loading). I have a piece of code that needs to check if employee.Employer
is null.
Since Employer
is non-nullable, naturally, I get "we can replace employee.Employer is null with true". Which isn't always the case.
The underlying issue is that entity is used to represent a domain object (where Employer is always assumed to exist), and a DTO for loading data.
Most of the code is in the "domain" segment of the application, so I would prefer to keep Employer as non-nullable; even though it is more correct to mark it as nullable - I don't want to have to check for null every time I touch it (I'd rather have NRE to indicate that somebody forgot to call .Include(e => e.Employer)
.
I can use a comment to suppress a warning, but want to check if there is an idiomatic way to handle this.