0

Using the following entities

public class User
{
    public Guid Id { get; set; }
    public string Username { get; set; }
}

public class GeneralEntity
{
    public Guid Id { get; set; }

    public User CreatedByUser { get; set; }

    public User DeletedByUser { get; set; }
}

How do I flatten this to the GeneralEntityDto below?

public class GeneralEntityDto
{
    public Guid Id { get; set; }

    public string CreatedByUsername { get; set; }

    public string DeletedByUsername { get; set; }
}

I have tried setting up my mappings as seen below but it fails with a complaint about "CreatedByUsername" and "DeletedByUsername" not being mapped.

protected void Configure()
{
    CreateMap<GeneralEntity, GeneralEntityDto>()
        .ForMember(dest => dest.CreatedByUsername, 
            opt => opt.MapFrom(src => src.CreatedByUser.Username))
        .ForMember(dest => dest.DeletedByUsername, opt => 
            opt.MapFrom(src => src.DeletedByUser.Username));
}
Tim Hays
  • 17
  • 1
  • 5

1 Answers1

1

You can use the naming convention that automapper provides. Basically if you include the exact string of the property name of the source Object you do not have to add ForMember() automapper is clever enough to do it automatically.

That means for example :

public class GeneralEntity
{
  public Guid Id { get; set; }

  public User CreatedBy { get; set; } // renaming just for simplicity

  public User DeletedBy { get; set; } // renaming just for simplicity
}



public class GeneralEntityDto
{
  public Guid Id { get; set; }

  public string CreatedByUsername { get; set; }

  public string DeletedByUsername { get; set; }
}

Reference also to these:

http://docs.automapper.org/en/stable/Flattening.html

AutoMapper TwoWay Mapping with same Property Name

kostas.kapasakis
  • 920
  • 1
  • 11
  • 26
  • Thanks for responding quickly. Is would prefer to keep my names the way they are. Is there a way to do this without changing my property names? – Tim Hays Jan 14 '19 at 16:41
  • Yes, you can create a custom naming convention. Like [this](https://github.com/AutoMapper/AutoMapper/blob/5e4a49dfa922ced68ca1189aa261351e997a651a/src/UnitTests/ReverseMapping.cs#L307). – Lucian Bargaoanu Jan 14 '19 at 18:09
  • I am not looking to create a new naming convention. I am looking to identify specifically what property of the child class to use for the mapping. – Tim Hays Jan 14 '19 at 18:46
  • That's the way to do it :) Of course if you want to solve only this specific case, you just need the configuration code you already have. – Lucian Bargaoanu Jan 14 '19 at 19:29
  • Nevermind, I have decided to write a custom static class for my DTO conversion. It is easier to maintain and understand for other developers who may need to make changes to the code in the future. Plus it is also debuggable. – Tim Hays Jan 14 '19 at 21:06
  • I will accept the first answer because I feel that if I decided to go read all the documentation I would eventually find the answer somewhere there. – Tim Hays Jan 14 '19 at 21:06