Search code examples
entity-frameworkautomapper

Automapper projections with Includable IQueryable


I am trying to use AutoMapper to project a database entity with include items to another domain entity:

IQueryable<DBTypeEntity> allItems = _dbContext.DBType
                                              .Include(e => e.A)
                                              .Include(e => e.B)
                                                  .ThenInclude(e => e.C)
                                              .Include(e => e.D)
                                                  .ThenInclude(ac => ac.E);

And using projections

var result = allItems.ProjectTo<DBTypeDomain>(_typeMapper.ConfigurationProvider);

Here are my mappings:

CreateMap<DBTypeEntity, DBTypeDomain>()
         .ForMember(e => e.A, opt => opt.Ignore())
         .ForMember(e => e.B, opt => opt.MapFrom(e => e.B))
         .ForMember(e => e.C, opt => opt.MapFrom(e => e.C))
         .ForMember(e => e.E, opt => opt.MapFrom(e => e.E))
         .ForMember(e => e.F, opt => opt.ResolveUsing<FResolver, ICollection<AssociatedFEntity>>(e => e.AssociatedFs))
         .ForMember(e => e.G, opt => opt.MapFrom(e => e.G));

Other types are straight property to property mappings with nothing special.

I have all the required profile mappings setup for the type mapper, however, I am getting a cryptic exception (Can't resolve this to queryable expression).

I am using EF Core 2.0 and Automapper 6.2.2

Any help please?


Solution

  • I managed to fix the issue. Includes are no longer needed when using projections (as in Automapper Docs). Custom resolver was replaced with a select statement from the set, the custom resolver was prohibiting using projections in AutoMapper for the IQueryable.