Open haharoi opened 5 months ago
Instead of trying to do this mapping all manually, have you considered using something like AutoMapper.Extensions.OData
?
I know this is not a direct answer to your question but hopefully it helps.
Instead of trying to do this mapping all manually, have you considered using something like
AutoMapper.Extensions.OData
?
No, I have not yet. I'm porting an existing project from NET Framework+EF to NET+EF Core. The project does not use AutoMapper and utilizing it seems to be a lot of work.
@haharoi
The underlying DB provider is Npgsql.EntityFrameworkCore.PostgreSQL. but I think it does not really matter.
This does matter quite a bit actually since the exception you are getting is coming from the provider not being able to translate the query.
Can you share the version of the Npgsql.EntityFrameworkCore.PostgreSQL
package you are using? And, if it's an older version, could you try upgrading it and testing to see if the problem persists?
Can you share the version of the
Npgsql.EntityFrameworkCore.PostgreSQL
package you are using? And, if it's an older version, could you try upgrading it and testing to see if the problem persists?
It is of the latest version, Npgsql.EntityFrameworkCore.PostgreSQL version 8.0.4
This does matter quite a bit actually since the exception you are getting is coming from the provider not being able to translate the query.
I just checked with SQL Server:
.UseSqlServer("data source=.;initial catalog=Users;persist security info=True;Integrated Security=True;TrustServerCertificate=True;multipleactiveresultsets=True")
and got the same error.
@haharoi you mentioned you are performing a migration right now. Did this same code work before with other versions of OData and EF? If so, could you also share that information?
Yes, the same code worked with EntityFramework, Version=6.4.4 and Microsoft.AspNet.OData, Version=7.4.1.0
@xuzhg can you please loop in EF Core folks to help investigate? It seems the issue could be with the client evaluation changing from EF to EF Core
@xuzhg can you please loop in EF Core folks to help investigate? It seems the issue could be with the client evaluation changing from EF to EF Core
@Roji any thoughts on this particular issue?
It could be related to https://github.com/dotnet/efcore/issues/27460. @maumar Any comments from your side.
The suggestion that worked there was to use 'HandleNullPropagation = false' - EF is generally good at handling null protection itself (there were some issues in the past and I think that's why OData tries to compensate). That would be my first suggestion.
https://github.com/dotnet/efcore/issues/27460#issuecomment-1043446872
It could be related to dotnet/efcore#27460. @maumar Any comments from your side.
This seems to be the similar issue indeed, and I studied it before submitting the issue.
The suggestion that worked there was to use 'HandleNullPropagation = false' - EF is generally good at handling null protection itself (there were some issues in the past and I think that's why OData tries to compensate). That would be my first suggestion.
Unfortunately, I cannot remove null checks completely, as I described above in the Additional context section in my initial post. The same User.toDto expression, for example, is used to project the optional property of other entities in the client code.
Assemblies affected ASP.NET Core OData 8.2.5
Describe the bug Expanding collection fails as the underlying EF Core cannot translate the expression constructed by OData.
The data model consits of two entities, UserDto and RoleDto.
Db entities and projections are:
The underlying DB provider is Npgsql.EntityFrameworkCore.PostgreSQL. but I think it does not really matter. When trying to get list of Users with expanded Roles with http://localhost:5070/Users?$expand=Roles, EF Core tries to compile the expression
and fails with
Expected behavior A list of UserDto entities, with Roles property
Additional context If the null check in User.toDto is removed, everything works in this particular case. By the null check cannot be removed in my actual application as the expression is used throughout the app and in some cases it is evaluated on client (not in database) and the null-check is required. Moreover, there are a number of similar expressions for other projections (hundreds of them).
Here is a sample project with application that demonstrates the described behavior pr01ODataModels.zip.
EDM (CSDL) Model