Linq projection returns object with defaults instead of null when db reference is null
Description
Example:
We have two entities: School and Janitor
School has a reference to Janitor
If we create a schol without a janitor and load the entity the school.Janitor property is null. If we project this to a dto in an expression school.Janitor is not null but all properties of the school.Janitor object have default values.
I'm not sure if this is caused by our mapping or a bug. I'll provide a test case to reproduce/analyze this behaviour.
Environment
None
Attachments
1
NH3938.zip
25 Jan, 2017
Activity
Show:
Oskar Berggren
February 4, 2017 at 3:25 PM
Not sure if it's reasonable to expect SchoolDto.Janitor to be null when you are explicitly creating a JanitorDto in the projection. It would be "null propagation" of some sort I guess. How can NHibernate know when to do this, in the general case? What if the JanitorDto is constructed from data found in several different referenced entities? Perhaps it null should be propagated if and only if all referenced properties used for initialization are null? That would still make the expression behave different compared to Linq2Objects. How does Linq2EF handle it?
Former user
January 25, 2017 at 11:55 AM
I have just uploaded a test to explain our problem. Just place it in the folder NHSpecificTest and run the test, it should fail.
If you comment out the following line in the provided test the Janitor property is null in one case and the test works:
.Select(e => new SchoolDto { Name = e.Name, Janitor = new JanitorDto { Name = e.Janitor.Name} })
Example:
We have two entities: School and Janitor
School has a reference to Janitor
If we create a schol without a janitor and load the entity the school.Janitor property is null. If we project this to a dto in an expression school.Janitor is not null but all properties of the school.Janitor object have default values.
I'm not sure if this is caused by our mapping or a bug. I'll provide a test case to reproduce/analyze this behaviour.