AutoMapper limit depth of mapping or map lazily

AutoMapper is great, saves a lot of time, but when I started looking at the performance of my application AutoMapper is responsible for performance loss.

I'm using lazy loading with NHibernate. Most of the time a need parent entity without needing to access child entities at all. In reality what happens is that AutoMapper tries to map as many relationships as possible causing NHibernate to lazy load all the child entities (I'm seeing SELECT N+1 happening all the time).

Is there way to limit how deep AutoMapper goes or is it possible for AutoMapper to map child entities lazily?

You could use the ignore method for associations you don't need to have loaded.

Mapper.CreateMap<User, UserDto>()
    .ForMember(dest => dest.LazyCollection, opt => opt.Ignore())
    .ForMember(dest => dest.AnotherLazyCollection, opt => opt.Ignore())
Mapper.CreateMap<UserProperty, UserPropertyDto>()
    .ForMember(dest => dest.PropertyLazyReference, opt => opt.Ignore());
return Mapper.Map<User, UserDto>(user);

For associations you know you will need in your dto, you should look at ways of fetching these more efficiently with the initial query, but that is a whole new problem.

nhibernate - AutoMapper limit depth of mapping or map lazily, You could use the ignore method for associations you don't need to have loaded. Mapper.CreateMap<User, UserDto>() .ForMember(dest => dest. Before and After Map Action; Understanding Your Mappings¶ AutoMapper creates an execution plan for your mapping.

Perhaps you should consider using two different dtos; one that includes the child entities, and one that doesn't. You can then return the proper dto from your service layer depending upon the context.

Procrastinator, If a have a full existing mapping between DTOs and entities but in some cases I how deep should you map? example: mapper.Map(order).MaxDepth(3);. — Similar kind of issues you could be facing when used with lazy  Configuration compilation¶ Because expression compilation can be a bit resource intensive, AutoMapper lazily compiles the type map plans on first map. However, this behavior is not always desirable, so you can tell AutoMapper to compile its mappings directly:

AutoMapper/AutoMapper, EF just does lazy loading which can result in horrible performance. Would it be possible to map expressions on the database entity and not the DTO One item is setting the child, but after max depth is done it won't set Mapping configuration -->> This is the fix I was looking for and proved to be correct:. We created a mapping from PersonDTO to Person, but we haven't told AutoMapper how to map between AddressDTO and Address. When AutoMapper comes across the same property name on the source and destination classes with different types, it will helpfully automatically attempt to map from one type to the other.

Automapper throws StackOverFlowException even with , Map functions, you may notice that the ORM will query all the fields of all the ProjectTo<OrderLineDTO>() will tell AutoMapper's mapping engine to emit a  Consider following mapping configuration CreateMap<A, ADto>(); With Repository.GetQueryableResponse().ProjectTo<ADto>(Mapper.ConfigurationProvider); ==> will map all properties, so class B and C will be retrieved from db and made available in class A and everything will be mapped on ADto

Queryable Extensions, One of these methods is to use another type map, where the source member type and destination member type are also configured in the mapping configuration  Setting a manual mapping configuration We can instruct Automapper to map a destination object's member to a specific source object's member. This is useful when the default mapping (which search for a member with the same name) doesn't produce the correct result.

Nested Mappings, 30.1 AutoMapper no longer creates maps automatically by default . Because expression compilation can be a bit resource intensive, AutoMapper lazily compiles the type map plans on This limits the reusability of resolvers, since the. Hi! I am pretty excited about this. AutoMapper already supports QueryableExtensions. using Map<Dto,Entity().ProjectTo() you can map an IQueryable to an IQueryable However as this mapping is made eager, applying any additioanl filters to the resulting IQueryable is not possible. I managed to create an MappingQueryProvider that does this lazily.

  • I'm having exact same problem. Good question, there are lots of customizations for AutoMapper, so customization will be needed.
  • I ended up using custom resolvers, thank you for your suggestion
  • The recent v2 release of AutoMapper has a .MaxDepth(int) limiter, but I'm not sure whether that's just for recursive object structures or can apply to just deep structures.