Unflatted with automapper

42 Views Asked by At

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?

1

There are 1 best solutions below

1
pfx On BEST ANSWER

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
            }
        ));