After mapping you will have mapped object without any references to source entity (which holds database context for lazy loading). Only property values are copied to destination entity. So you will not be able to do any lazy-loading without source entity.
Actually lazy loading works just fine for you - and it's occur during mapping process. You specified mappings for lazy-loaded properties of your entity, and mapper tries to get those values. That results in lazy-loading all navigation properties which you have configured for mapping. This is very inefficient. To disable lazy-loading during mapping you can ignore navigation properties in mapping configuration. E.g. if you have customer with lazy-loaded orders:
Mapper.CreateMap<Customer, CustomerDto>()
.ForMember(s => s.Orders, m => m.Ignore());
Or remove Orders
property from your destination entity CustomerDto
. If you need to have CustomerDto
instance with orders inside, then best option is to do eager loading of orders, to avoid additional queries.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…