I have this simple class
public class Store {
public int Id { get; set; }
//Other properties
[JsonIgnore]
public ICollection<Product> Products { get; set; }
}
and my DTO
public class StoreDetails {
public int Id { get; set; }
//Other properties
public ICollection<Product> Products { get; set; }
}
and my Product class:
public class Product {
public int? Id { get; set; }
//Other properties
public ICollection<ProductAttribute> ProductAttributes { get; set; }
}
And my Mapping looks like that:
var storeDetails = await _context.Stores
.Include(s => s.Products)
.ThenInclude(p => p.ProductAttributes)
.ProjectTo<StoreDetails>(new MapperConfiguration(c => c.CreateProfile("TEST", e => {
e.CreateMap<Store, StoreDetails>();
})))
.SingleOrDefaultAsync(p => p.Id == id);
Everything looks fine in the Store object, but in StoreDetails ProductAttributes is null every time.
Why AutoMapper is not Mapping mapping second level navigation property using ProjectTo?
NOTE: I'm using AutoMapper 8.1.1.
Productis the same class for both so there is no need to perform any mapping on it. Did you try running it without usingThenInclude(p => p.ProductAttributes)? - S SchulzeThenInclude- MorasiuAssignableExpressionBinderandEnumerableExpressionBinderare hardly forcing projection of the source expression. - Ivan StoevToListbecause theAssignableExpressionBindercomes before theEnumerableExpressionBinder. Changing the order would accomplish what you want, but, as I've said, it works for me without it. - Lucian Bargaoanu