You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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 the PhoneNumber is the SQL itself.
Sample:
When select new Wrapper is used the PhoneNumber is null, however when using select new it is returned correctly.
using Microsoft.EntityFrameworkCore;using System.Text.Json;intapartmentId=0;awaitusing(vardb=new AppDbContext()){varapartment=new Apartment();
db.Add(apartment);await db.SaveChangesAsync();varperson=new Person
{ApartmentId= apartment.Id,PhoneNumber=new("old")};
db.Add(person);await db.SaveChangesAsync();apartmentId= apartment.Id;}awaitusing(vardb=new AppDbContext()){varquery=(fromapartmentin db.Apartments
let persons =(fromotherApartmentin db.Apartments
frompersonin(frompin db.Persons
where p.ApartmentId == otherApartment.Id
// Removing the Wrapper type and using an anonymous type correctly returns the PhoneNumber.selectnew Wrapper
{Person=p})where otherApartment.Id == apartment.Id
selectperson).ToList()where apartment.Id ==apartmentIdselectnew{
apartment.Id,
persons
});// PhoneNumber is not queried in the query string.
Console.WriteLine(query.ToQueryString());varresult=await query.ToListAsync();// PhoneNumber is null even though it shouldn't be.
Console.WriteLine(JsonSerializer.Serialize(result));}publicclassAppDbContext:DbContext{publicDbSet<Apartment> Apartments {get;set;}publicDbSet<Person> Persons {get;set;}protectedoverridevoidOnConfiguring(DbContextOptionsBuilderoptionsBuilder)=> optionsBuilder.UseNpgsql("Host=127.0.0.1;Port=5433;Database=efcore-owned-entity;Username=postgres;Password=developer");protectedoverridevoidOnModelCreating(ModelBuildermodelBuilder){
modelBuilder.Entity<Apartment>(e =>{ e.HasMany(a => a.Persons).WithOne().HasForeignKey(p => p.ApartmentId);});
modelBuilder.Entity<Person>(e =>{ e.OwnsOne(p => p.PhoneNumber);});}}publicclassApartment{publicintId{get;set;}publicList<Person> Persons {get;set;}}publicclassPerson{publicintId{get;privateset;}publicPhoneNumberPhoneNumber{get;set;}publicintApartmentId{get;set;}}publicrecordPhoneNumber(string?Number);publicclassWrapper{publicPersonPerson{get;set;}}
Results for the two WriteLines:
SQL:
-- @__apartmentId_0='6'
SELECT a."Id", t."Id", t."ApartmentId", t."Id0"
FROM "Apartments" AS a
LEFT JOIN (
SELECT p."Id", p."ApartmentId", a0."Id" AS "Id0"
FROM "Apartments" AS a0
INNER JOIN "Persons" AS p ON a0."Id" = p."ApartmentId"
) AS t ON a."Id" = t."Id0"
WHERE a."Id" = @__apartmentId_0
ORDER BY a."Id", t."Id0"
When changing the projection to an anonymous object this is the expected output:
-- @__apartmentId_0='7'
SELECT a."Id", t."Id", t."ApartmentId", t."PhoneNumber_Number", t."Id0"
FROM "Apartments" AS a
LEFT JOIN (
SELECT p."Id", p."ApartmentId", p."PhoneNumber_Number", a0."Id" AS "Id0"
FROM "Apartments" AS a0
INNER JOIN "Persons" AS p ON a0."Id" = p."ApartmentId"
) AS t ON a."Id" = t."Id0"
WHERE a."Id" = @__apartmentId_0
ORDER BY a."Id", t."Id0"
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.
The text was updated successfully, but these errors were encountered: