Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1071883
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T20:46:28+00:00 2026-05-16T20:46:28+00:00

I have the following Dto and entity with a nested sub entity. public class

  • 0

I have the following Dto and entity with a nested sub entity.

public class Dto
{
    public string Property { get; set; }
    public string SubProperty { get; set; }
}

public class Entity
{
    public string Property { get; set; }
    public SubEntity Sub { get; set; }
}

public class SubEntity
{
    public string SubProperty { get; set; }
}

How can I set up a mapping with AutoMapper that will allow me to update an existing instance of Entity with the values from a Dto.

I’m using Mapper.Map(dto, entity) to update an existing entity but when I try to map Dto.SubProperty to Entity.Sub.SubProperty I get an exception for “must resolve to top-level member. Parameter name: lambdaExpression”.

If I create a mapping from Dto to SubEntity using FromMember then Entity.Sub gets replaced with a new instance of SubEntity but that’s not what I want. I just want it to update the properties of the existing instance of SubEntity on the Sub property of Entity.

How can I achieve this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T20:46:28+00:00Added an answer on May 16, 2026 at 8:46 pm

    I solved it by using a combination of the ResolveUsing<T>() method and implementing IValueResolver and the ConvertUsing<T>() method and implementing ITypeConverter<TSource,TDestination>.

    Some of my mapping scenarios are more complicated than normal including bidirectional mapping and nested classes and nested collections. The above helped me to solve them.


    EDIT

    As requested, I’ve included an example solution. This example is much simpler than the actual types I was dealing with.

    using System;
    using AutoMapper;
    
    namespace TestAutoMapperComplex
    {
        public class Dto
        {
            public string Property { get; set; }
            public string SubProperty { get; set; }
        }
    
        public class Entity
        {
            public string Property { get; set; }
            public SubEntity Sub { get; set; }
        }
    
        public class SubEntity
        {
            public string SubProperty { get; set; }
        }
    
        static class MapperConfig
        {
            public static void Initialize()
            {
                Mapper.CreateMap<Dto, Entity>()
                    .ForMember(entity => entity.Sub, memberOptions =>
                        memberOptions.MapFrom(dto => dto));
                Mapper.CreateMap<Dto, SubEntity>();
            }
        }
    
        static class MapperConfig2
        {
            private class MyResolver : IValueResolver
            {
    
                public ResolutionResult Resolve(ResolutionResult source)
                {
                    var destinationSubEntity = ((Entity)source.Context.DestinationValue).Sub;
    
                    Mapper.Map((Dto)source.Value, destinationSubEntity);
    
                    return source.New(destinationSubEntity, typeof(SubEntity));
                }
            }
    
            public static void Initialize()
            {
                Mapper.CreateMap<Dto, Entity>()
                    .ForMember(entity => entity.Sub, memberOptions =>
                        memberOptions.ResolveUsing<MyResolver>());
                Mapper.CreateMap<Dto, SubEntity>();
            }
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                MapperConfig.Initialize();
    
                var dto = new Dto {Property = "Hello", SubProperty = "World"};
                var subEntity = new SubEntity {SubProperty = "Universe"};
                var entity = new Entity {Property = "Good bye", Sub = subEntity};
    
                Mapper.Map(dto, entity);
    
                Console.WriteLine(string.Format("entity.Property == {0}, entity.Sub.SubProperty == {1}",
                    entity.Property, entity.Sub.SubProperty));
                Console.WriteLine(string.Format("entity.Sub == subEntity: {0}", 
                    entity.Sub == subEntity));
    
            }
        }
    }
    

    If you run the example, which is using MapperConfig, you’ll get the following output:

    entity.Property == Hello, entity.Sub.SubProperty == World
    entity.Sub == subEntity: False
    

    The string properties all get updated as one would want them to, but entity.Sub gets replaced with a new instance of SubEntity which is no good for when you are wanting to update entities for an ORM that will be persisted to a database.

    If you modify Main so that MapperConfig2 is used instead, you’ll still have the string properties updated as before, but, entity.sub still has the same instance of SubEntity that it had before. Running the example with MapperConfig2 gives this output:

    entity.Property == Hello, entity.Sub.SubProperty == World
    entity.Sub == subEntity: True
    

    The key difference in MapperConfig2 is that ResolveUsing is used along with MyResolver to preserve the value of entity.Sub.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.