1
votes

We are trying to implement latest OData in our project. We are using EF Core, and what we are trying to do is, map the domain set to new IQueryable of Dto Models using custom expression function as mapper. After the apply of the OData filters, the query is broken and cannot be executerd.

Assemblies affected

Microsoft.AspNetCore.OData v7.3.0

Microsoft.EntityFrameworkCore v3.1.2

.Net Core 3.1 WebApi Project

Reproduce steps IQueryable that is returned in the controller

_dbContext.PurchaseTypes.Select(Mapper.ToDto())

Expression function used for mapping

public static Expression<Func<PurchaseType, PurchaseTypeDto>> ToDto()
{
    return domain => new PurchaseTypeDto
    {
        Id = domain.Id,
        Code = domain.Code
    };
}

In the controller, we have service call (which returns IQueryable) and we are applying the ODataQueryOptions opts.

[HttpGet]
public async Task<IActionResult> Get(ODataQueryOptions<PurchaseTypeDto> opts)
{
    var query = opts.ApplyTo(_purchaseTypeService.GetAll()) as Queryable<PurchaseTypeDto>;

    var result = await query.ToListAsync();

    return Ok(result);
}

Expected result When the url is accessed https://localhost:44361/odata/PurchaseType?%24filter=Id%20eq%202, execute the query and result is returned

Actual result The created query could not be executed

InvalidOperationException: The LINQ expression
'DbSet
.Where(p => new PurchaseTypeDto(
p.Id,
p.Code
).Id == __TypedProperty_0)' 
could not be translated.
1

1 Answers

1
votes

It is EF Core bug. https://github.com/dotnet/efcore/issues/19087 for now you can downgrade EF Core to 2.2.6. Expected fix with version 3.1.4