Search code examples
c#entity-frameworkodata

OData query against DTO, execute against EF


On a project I am working on, we want to use OData to query our data from the frontend. The trouble is that the frontend ODataControllers are exposing the DTOs of the project, and it constructs the queries against the DTOs and not the entities from the EntityFramework.

The data is mapped between DTO and Entity by AutoMapper.

Is there any way to make the OData controllers map the queries so they query against EF entities and not DTOs?


Solution

  • The AutoMapper IQueryable extension is designed to achieve this result : mapping OData queries on DTOs/POCOs to EF Queries on entities.

    For example :

    In some scenarios, such as OData, a generic DTO is returned through an IQueryable controller action. Without explicit instructions, AutoMapper will expand all members in the result. To control which members are expanded during projection, set ExplicitExpansion in the configuration and then pass in the members you want to explicitly expand

    You may give it a try.