Using OData with Automapper and EF core to expose the data model below,
- Customer
- CustomerMetric: CustomerLevel,VIPFlag,other fields...
Customer has one to many relationship with CustomerMetric. Goal is to for the Customer controller to expose CustomerLevel,VIPFlag in the first matching row from the CustomerMetric table.
What I did in the map config,
- .ForMember(dest => dest.CustomerLevel, opt => opt.MapFrom(src => src.CustomerMetric.FirstOrDefault().Level))
- .ForMember(dest => dest.VIPFlag , opt => opt.MapFrom(src => src.CustomerMetric.FirstOrDefault().VIPFlag ))
This is all good, values are populated correctly. However, automapper are triggering two calls to the database, with 'select top 1 CustomerLevel' and another one with 'select top 1 VIPFlag'.
How to make it possible that with one call to fetch both fields rather than two calls for each individual field? I tried .Include on the controller it does not work..
Thanks!!