1

I have following objects:

class FirstEntity {
    public string Id;
    public string Name;
    public List<SecondEntity> SecondEntities;

    public Guid CreatorId;
    public Guid LastModifierId;
}

class SecondEntity {
    public string Id;
    public string AnotherName;

    public Guid CreatorId;
    public Guid LastModifierId;
}

class FirstDto {
    public string Id;
    public string Name;
    public List<SecondDto> SecondEntities;
}

class SecondDto {
    public string Id;
    public string AnotherName;
}

My mapping is as follows:

CreateMap<FirstEntity, FirstDto>()
.ReverseMap()
.ForAllMembers(opt => opt.Condition((src, dest, srcMember) => srcMember != null)); 
     
CreateMap<SecondEntity, SecondDto>()
.ReverseMap()
.ForAllMembers(opt => opt.Condition((src, dest, srcMember) => srcMember != null));

I am mapping like below:

var firstEntity = _firstEntityDal.SingleOrDefault(w => w.Id == someId);
automapper.Map(firstDto, firstEntity);

Before mapping my firstEntity and firstDto is like below:

firstEntity{
  Id: "54bfbfaa-1a8a-4239-a777-f504f90eaa79",
  Name: "First Entity",
  SecondEntities: [
    {
        Id: "191ca292-ff5b-4dae-881b-c883641296f4"
        AnotherName: "SecondEntity 1",
        CreatorId: "76af4a15-c73c-4d0a-a5ff-8b44a50f781b",
        LastModifierId: "7971e3a5-e6ca-4b28-b631-0928b7455167"
        },
        {
        Id: "745d4e38-e82b-47cb-95fa-f6a522d6eb57"
        AnotherName: "SecondEntity 2",
        CreatorId: "76af4a15-c73c-4d0a-a5ff-8b44a50f781b",
        LastModifierId: "7971e3a5-e6ca-4b28-b631-0928b7455167"
        },
        {
        Id: "685415c7-2d68-485c-a9de-1433e17e6ebb"
        AnotherName: "SecondEntity 3",
        CreatorId: "76af4a15-c73c-4d0a-a5ff-8b44a50f781b",
        LastModifierId: "7971e3a5-e6ca-4b28-b631-0928b7455167"
        }
    ],
    CreatorId: "76af4a15-c73c-4d0a-a5ff-8b44a50f781b",
    LastModifierId: "7971e3a5-e6ca-4b28-b631-0928b7455167"
}

firstDto{
  Id: "54bfbfaa-1a8a-4239-a777-f504f90eaa79",
  Name: "First Entity",
  SecondEntities: [
    {
        Id: "191ca292-ff5b-4dae-881b-c883641296f4"
        AnotherName: "SecondEntity 1"
        },
        {
        Id: "745d4e38-e82b-47cb-95fa-f6a522d6eb57"
        AnotherName: "SecondEntity 2"
        },
        {
        Id: "685415c7-2d68-485c-a9de-1433e17e6ebb"
        AnotherName: "SecondEntity 3"
        }
    ]
}

After mapping firstEntity turns to this:

   firstEntity {
      Id: "54bfbfaa-1a8a-4239-a777-f504f90eaa79",
      Name: "First Entity",
      SecondEntities: [
        {
            Id: "191ca292-ff5b-4dae-881b-c883641296f4"
            Name: "SecondEntity 1",
            CreatorId: "00000000-0000-0000-0000-000000000000",
            LastModifierId: "00000000-0000-0000-0000-000000000000"
            },
            {
            Id: "745d4e38-e82b-47cb-95fa-f6a522d6eb57"
            Name: "SecondEntity 2",
            CreatorId: "00000000-0000-0000-0000-000000000000",
            LastModifierId: "00000000-0000-0000-0000-000000000000"
            },
            {
            Id: "685415c7-2d68-485c-a9de-1433e17e6ebb"
            Name: "SecondEntity 3",
            CreatorId: "00000000-0000-0000-0000-000000000000",
            LastModifierId: "00000000-0000-0000-0000-000000000000"
            }
        ],
        CreatorId: "76af4a15-c73c-4d0a-a5ff-8b44a50f781b",
        LastModifierId: "7971e3a5-e6ca-4b28-b631-0928b7455167"
    }

The problem is Guid fields CreatorId and LastModifierId remains the same for firstEntity (as expected) but these fields for the elements in SecondEntities gets their default values.

What can be the reason for this? How can I prevent this?

Kubra
  • 188
  • 8
  • Can you add some json style representation of what you're getting now vs what you're expecting? Or even a screenshot of your debug window showing what you get now, with the problem data circled and a description of what it should be. It's a little difficult to understand the problem. It sounds like your complaint is that automapper is creating a List with the same number of entries as the List has but all the values for Id and Name are default instead of values populated from the entries in List – Caius Jard Dec 01 '21 at 06:42
  • I have added more explanation @CaiusJard – Kubra Dec 01 '21 at 07:33
  • Research `AutoMapper.Collection`. – Lucian Bargaoanu Dec 01 '21 at 08:16

1 Answers1

0

After mapping firstEntity turns to this:

Oh, I see.. I guess the problem is you're hoping AutoMapper will see that you have an existing SecondEntity with ID 191ca292-ff5b-4dae-881b-c883641296f4, and you have an incoming SecondDto with ID 191ca292-ff5b-4dae-881b-c883641296f4 so AutoMapper should look up the existing SecondEntity in the list by ID, and then update the existing instance with just the new values from SecondDto ..

..AutoMapper doesn't have any automatic facility for that as far as I know. You're seeing your Guids reset to 00000... because basically AutoMapper clears the List and fills it with new SecondEntity objects created from the information in a SecondDto (which lacks the creator/modifier IDs)

One route would be to set an Ignore on the SecondEntities list so that AutoMapper leaves it alone, and then use a loop that does the lookup, and use AutoMapper to map the direct instances you want, e.g.

foreach(var sd in myFirstDto.SecondEntitites)
  automapper.Map(sd, myFirstEntity.SecondEntities.Single(se => se.Id == sd.Id));

You can also create a custom converter and tell automapper to use it, see Map list to existing list in Automapper using a key for more info

Caius Jard
  • 72,509
  • 5
  • 49
  • 80
  • Oh, ok. Yes I was expecting guids in the list elements remain the same, as in outer object. For now, I solve my problem without an automapper solution, but this will be useful anyways. Thank you! :) – Kubra Dec 01 '21 at 08:56