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

  • Home
  • SEARCH
  • 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 9097601
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T00:09:21+00:00 2026-06-17T00:09:21+00:00

Using AutoMapper: When mapping nested collections, I expect any unmapped properties to have their

  • 0

Using AutoMapper: When mapping nested collections, I expect any unmapped properties to have their original values retained. Instead, they are being set to null.

Example:
I have these four classes
(note that Test2Child has the Name property, while Test1Child does not):

public class Test1
{
    public List<Test1Child> Children { get; set; }
}
public class Test2
{
    public List<Test2Child> Children { get; set; }
}
public class Test1Child
{
    public int Value { get; set; }
}
public class Test2Child
{
    public string Name { get; set; }
    public int Value { get; set; }
}

…and a simple mapping setup.

Mapper.CreateMap<Test1, Test2>();
Mapper.CreateMap<Test1Child, Test2Child>().ForMember(m => m.Name, o => o.Ignore());
Mapper.AssertConfigurationIsValid();    // Ok

I want the original value of Test2Child.Name to be retained during mapping…. I expect this to be the default behaviour for any unmapped properties.

When I map directly from Test1Child to Test2Child, it works fine; Value is mapped and Name is retained:

var a = new Test1Child {Value = 123};
var b = new Test2Child {Name = "fred", Value = 456};
Mapper.Map(a, b);
Assert.AreEqual(b.Value, 123);    // Ok
Assert.AreEqual(b.Name, "fred");  // Ok

When the mapping is for a nested collection (List<Test1Child> to List<Test2Child>),
Value is mapped correctly… but the original value for Name is lost!

var c = new Test1 { Children = new List<Test1Child> { new Test1Child { Value = 123 } } };
var d = new Test2 { Children = new List<Test2Child> { new Test2Child { Name = "fred", Value = 456 } } };
Mapper.Map(c, d);
Assert.AreEqual(d.Children[0].Value, 123);    // Ok
Assert.AreEqual(d.Children[0].Name, "fred");  // FAILS! Name is null.

How do I fix 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-06-17T00:09:23+00:00Added an answer on June 17, 2026 at 12:09 am

    As mentioned in comments of @MightyMuke’s answer, @PatrickSteele makes a good point here: maybe it doesn’t make sense to automatically map each item from the source list to the destination list…. i.e. “But what if one list has 3 and the other list has 5?“

    In my case, I know that the source and dest lists will always have the same length, and (importantly) the Nth item in the source list is always the direct counterpart to the Nth item in the dest list.

    So, this works, but I don’t feel good about myself….

    Mapper.CreateMap<Test1, Test2>()
        .ForMember(m => m.Children, o => o.Ignore())
        .AfterMap((src, dest) =>
            {
                for (var i = 0; i < dest.Children.Count; i++)
                    Mapper.Map(src.Children[i], dest.Children[i]);
            });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a rather large object with lots of properties. I am using Automapper
I am using .NET mapping library AutoMapper in my application, and I have a
I am using AutoMapper to datareader using code as discussed below http://elegantcode.com/2009/10/16/mapping-from-idatareaderidatarecord-with-automapper/ I see
Any idea how I can tell AutoMapper to resolve a TypeConverter constructor argument using
hey guys, I'm using automapper version 1.1.0.188 In my AutoMapper.Configure I'm mapping Entities to
I have recently started using automapper and it has work fine for me so
I have an AutoMapper profile which currently depends on UrlHelper for the following mapping:
I am using NHibernate 3.1.0.4000 and AutoMapper 2.0.0.0 in a WCF. I have a
I'm having a really difficult time mapping from a DynamicObject using Automapper -- the
I have been using automapper pretty successfully lately but i have come across a

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.