Search code examples
automapperautomapper-11

Unflatted with automapper


Let's say I have an EF Core model with these three properties:

public int Id { get; set; }
public string? ApplicationName { get; set; }
public string? ApplicationVersion { get; set; }

and in the automapper output, after calling ProjectTo I want it to map to Output:

public NameAndVersion {
    public string? Name { get; set; }
    public string? Version { get; set; }
}

public Output {
    public int Id { get; set; }
    public NameAndVersion? Application { get; set; }
}

is there a way to accomplish that?


Solution

  • Although ProjectTo has a restricted list of features, this one is can be set up via a regular MapTo.

    CreateMap<YourEFmodel, Output>()
        .ForMember(
            dest => dest.Application,
            opt => opt.MapFrom(
                src => new NameAndVersion 
                {
                    Name = src.ApplicationName,
                    Version = src.ApplicationVersion
                }
            ));