We are using EF Core within the Hot Chocolate framework to access some SQL databases. To overcome the issue with computed fields (described here) we make use of DTOs.
This works perfectly fine in the example below. When querying for "Foo", the "GetFoo" method is called which created a "fooDTO" object with the computed parameter.
[UseProjection]
[UseFiltering]
[UseSorting]
public IQueryable<fooDTO> GetFoo([Service] ApiDbContext context) {
return context.fooSet.AsEnumerable().Select(f => CreateFooDTO(f)).AsQueryable();
}
private static fooDTO CreateFooDTO(foo f){
return new fooDTO{
param1 = f.param1,
param2 = f.param2,
computedParam3 = param2 - param1,
};
}
However, things go wrong when we want use DTOs when querying for "Bar" referring "Foo":
{
bar {
foo
}
}
For that case we provided the following methods:
[UseProjection]
[UseFiltering]
[UseSorting]
public IQueryable<barDTO> GetBar([Service] ApiDbContext context){
return context.barSet.AsEnumerable().Select(b => CreateBarDTO(b)).AsQueryable();
}
private static barDTO CreateBarDTO(bar b){
return new barDTO{
param1 = b.param1,
param2 = b.param2,
param3 = CreateFooDTO(b.foo)
};
}
Here "b.foo" seems to be always null, meaning that fooDTO is never materialized.
My questions:
- Is this problem related to Hot Chocolate or EF Core?
- How to properly use DTOs in this case?