Joe Ricklefs
Joe Ricklefs

Reputation: 660

Automapper Finding Not Mapped properties

Using Automapper for a project, just mapping 2 objects to each other, nothing fancy. I must have something configured incorrectly because AutoMapper keeps saying that there are UnMapped Properties.
Here's the AutoMapper config.

var mapperConfig = new MapperConfiguration(cfg => {cfg.CreateMap<SrcObj, DestObj>()
                .ForMember(dest => dest.Level, opt => opt.MapFrom(src => src.lvl));}
mapperConfig.AssertConfigurationIsValid();

SrcObj

public  class SrcObj
{
    public int Id { get; set; }

    public int ParentNode { get; set; }

    public string Controller { get; set; }

    public string Action { get; set; }

    public string DisplayName { get; set; }

    public string Description { get; set; }

    public bool? IsActive { get; set; }

    public string AreaName { get; set; }

    public int? DisplayOrder { get; set; }

    public Int64 Type{ get; set; }

    public int lvl { get; set; }
}

DestObj

public  class DestObj
{
    public int Id { get; set; }

    public int ParentNode { get; set; }

    public string Controller { get; set; }

    public string Action { get; set; }

    public string DisplayName { get; set; }

    public string Description { get; set; }

    public bool? IsActive { get; set; }

    public string AreaName { get; set; }

    public int? DisplayOrder { get; set; }

    public Int64 Type{ get; set; }

    public int Level { get; set; }
}

And the Implementation:

var items  = await _context.Database.SqlQuery<SrcObj>($"EXEC spGenerateMenu {app1}").ToListAsync();
var rslt = _mapper.Map<DestObj>(items);

and the error:

{"\nUnmapped members were found. Review the types and members below.\nAdd a custom mapping expressio...}

The error actually lists every member of the DestObj. Not sure what I'm missing. probably something simple

Upvotes: 0

Views: 1145

Answers (1)

Peter B
Peter B

Reputation: 24270

Because your source is a List, you need to map it also to a List:

var rslt = _mapper.Map<List<DestObj>>(items);

Upvotes: 1

Related Questions