Home > database >  Exclude all parent properties when using IncludeMembers
Exclude all parent properties when using IncludeMembers

Time:06-29

public sealed class LookupDetails
{
    public long Id { get; set; }

    public string Name { get; set; }
}

public sealed class File
{
    public long Id { get; set; }

    public string Name { get; set; }
}

public sealed class UserFile
{
    public long Id { get; set; }

    public File File { get; set; }
}

CreateMap<UserFile, LookupDetails>()
   .IncludeMembers(x => x.File);

CreateMap<File, LookupDetails>();

I would like to make it work as projection, UserFile.File.Id => LookupDetails.Id and UserFile.File.Name => LookupDetails.Name, but it takes Id from UserFile.Id.

I've tried ForAllMembers(x => x.Ignore()) and ForAllOtherMembers(x => x.Ignore()) - does not help.

Also, I'm using queryable extensions, so I'm limited to expression-based configuration.

CodePudding user response:

I think you need to map that manually? Does this help?

CreateMap<UserFile, LookupDetails>()
    .ForMember(lookupDetails => lookupDetails.Id, options => options.MapFrom(userFile => userFile.File.Id))

CodePudding user response:

You're misusing IncludeMembers. You just need

CreateMap<UserFile, LookupDetails>().ConvertUsing((s, _, context) => context.Mapper.Map<LookupDetails>(s.File));
  • Related