0
votes

Using OData with Automapper and EF core to expose the data model below,

  1. Customer
  2. 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!!

1

1 Answers

0
votes

You can try grouping those two in a struct or class and then map the whole thing. That would work in EF6, I'm not sure about EF core. See this.