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 8206749
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T08:46:38+00:00 2026-06-07T08:46:38+00:00

I have a following class structure: public class A { public bool Property1 {

  • 0

I have a following class structure:

public class A
{
    public bool Property1 { get; set; }
    public bool Property2 { get; set; }
}

public class ContainerForA
{
    public A A { get; set; }
}

public class A1
{
    public bool Property1 { get; set; }
}

public class ContainerForA1
{
    public A1 A { get; set; }
}

I create a mapping for this set of classes:

Mapper.CreateMap<A1, A>();
Mapper.CreateMap<ContainerForA1, ContainerForA>();

I create an instance of this set of classes:

        var cnt_a = new ContainerForA()
                        {
                            A = new A()
                                    {
                                        Property1 = false,
                                        Property2 = true
                                    }
                        };

        var cnt_a1 = new ContainerForA1()
                         {
                             A = new A1()
                                     {
                                         Property1 = true
                                     }
                         };

If I call Mapper.Map(cnt_a1.A, cnt_a.A) I’m getting the result I was expecting: both properties (Property1 and Property2) of object cnt_a are true

But if I call Mapper.Map(cnt_a1, cnt_a) I’m getting true for Property1 and false for Property2. Could someone explain me why? And is there any option for me to declare my mappings in the way so I won’t lose properties that are present in my destination object but are NOT in my source object?

  • 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-07T08:46:39+00:00Added an answer on June 7, 2026 at 8:46 am

    I would guess that when you map from ContainerForA1 to ContainerForA, that when mapping the property A, it creates a new instance of A for ContainerForA rather than using the existing one. This will use the default values for all of the properties, which is false for a bool.

    How do we work around this?

    First, we need to tell AutoMapper to not overwrite the property A on ContainerForA. To do that, we will tell AutoMapper to ignore the property.

    Mapper.CreateMap<ContainerForA1, ContainerForA>()
        .ForMember(cForA => cForA.A, option => option.Ignore());
    

    Now we need to update A manually, using AfterMap

    Mapper.CreateMap<ContainerForA1, ContainerForA>()
        .ForMember(cForA => cForA.A, option => option.Ignore())
        .AfterMap((cForA1, cForA) => Mapper.Map(cForA1.A, cForA.A));
    

    You will probably want to add some checks to the AfterMap method to ensure that cForA.A is not null. I’ll leave that for you.

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

Sidebar

Related Questions

I have this following structure: public class Dummy { public string Name { get;
I have following structure: class Employee { public long Id { get; set; }
I have the following class structure: class TestCase { public IList<Step> Steps { get;
I have the following class structure: public class Result { protected int Code {get;
I have the following class structure public class Outer{ private Mapper a; .... private
I have following code: public abstract class Operand<T> { public T Value { get;
I have following class structure public class Limit { public double MinSelectedValue; public double
I have the following class structure: public class A : AInterface { } public
I have the following class structure: public class Fruit { } public class Apple
I have The Following Structure public class GraphData { private List<RecordPerDay> recordPerDay; public List<RecordPerDay>

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.