Open TwentyFourMinutes opened 4 months ago
i'm able to reproduce this, problem is in nav expansion, it doesn't peek into the Wrapper to generate Include expressions for SharedTypeEntityExpandingExpressionVisitor to expand it later. Workaround is, as @TwentyFourMinutes pointed out, to not use the wrapper type but regular anonymous type instead.
expression tree before translation with the wrapper type:
DbSet<Apartment>()
.Where(a => a.Id == __apartmentId_0)
.Select(a => new {
Id = a.Id,
persons = DbSet<Apartment>()
.SelectMany(
collectionSelector: a0 => DbSet<Person>()
.Where(p => p.ApartmentId == a0.Id)
.Select(p => new Wrapper{ Person = p }
),
resultSelector: (a0, c) => new TransparentIdentifier<Apartment, Wrapper>(
Outer = a0,
Inner = c
))
.Where(ti => ti.Outer.Id == a.Id)
.Select(ti => ti.Inner)
.ToList()
})
without wrapper type:
DbSet<Apartment>()
.Where(a => a.Id == __apartmentId_0)
.Select(a => new {
Id = a.Id,
persons = DbSet<Apartment>()
.SelectMany(
collectionSelector: a0 => DbSet<Person>()
.Where(p => p.ApartmentId == a0.Id)
.Select(p => new { Person = p }),
resultSelector: (a0, c) => new TransparentIdentifier<Apartment, <>f__AnonymousType23<Person>>(
Outer = a0,
Inner = c
))
.Where(ti => ti.Outer.Id == a.Id)
.Select(ti => new { Person = Include(
Entity: ti.Inner.Person,
Navigation: PhoneNumber, EF.Property<PhoneNumber>(ti.Inner.Person, "PhoneNumber") })
.ToList()
})
When nesting queries, with entities that hold owned entities, some queries no longer return the owned entity when being projected into a typed wrapper. Surprisingly when using an anonymous object everything works as expected. Also note that this doesn't seem to be related to the materializer, as the
ToQueryString
won't return thePhoneNumber
is the SQL itself.Sample:
select new Wrapper
is used thePhoneNumber
is null, however when usingselect new
it is returned correctly.Results for the two WriteLines:
SQL:
JSON:
When changing the projection to an anonymous object this is the expected output:
I am using the Npgsql adapter as well version 8.0.4 for all packages. See the repo for a Ready2Run sample https://github.com/TwentyFourMinutes/EFCoreOwnedEntityNotLoaded.