Search code examples
c#.netasp.net-mvcautomapperautomapper-2

Automapper Model > ViewModel Mapping Exception


I have the following Models and ViewModels (edited for brevity):

public class Advert
{
    public virtual long Id { get; set; }
    public virtual string Title { get; set; }
    public virtual string Descrip { get; set; }
    public virtual IList<AdvertImage> AdvertImages { get; set; }
}

public class AdvertImage
{
    public virtual byte DisplayOrder { get; set; }
    public virtual string Filename { get; set; }
    public virtual string UrlDirectoryRoot { get; set; }
    public virtual long FilesizeBytes { get; set; }

    public virtual Advert Advert { get; set; }
}

public class AdvertImageViewModel
{
    public virtual string Filename { get; set; }
    public virtual byte DisplayOrder { get; set; }
}

public class ListAdvertViewModel
{
    public long Id { get; set; }
    public virtual string Title { get; set; }
    public virtual string Descrip { get; set; }
    public AdvertImageViewModel AdvertImage { get; set; }
}

I have the following AutoMapper mappings:

Mapper.CreateMap<AdvertImage, AdvertImageViewModel>();

Mapper.CreateMap<Advert, ListAdvertViewModel>()
                .ForMember(d => d.AdvertImage, o => o.MapFrom(s => (s.AdvertImages == null ? null : s.AdvertImages.First())));

One of my controller methods then performs the following, to retrieve a list of Adverts (of which each Advert may have either any number of AdvertImages):

var ads = _advertService.GetAdverts();

Then I attempt to map these adverts into my ListAdvertViewModel ViewModel class

var mappedAds = Mapper.Map<IList<Advert>, IList<ListAdvertViewModel>>(ads);

This falls over though if one of the Adverts returned (in the 'ads' variable) has some images to map. The error that I get is:

[InvalidOperationException: Sequence contains no elements]
   System.Linq.Enumerable.First(IEnumerable`1 source) +498
   AutoMapper.DelegateBasedResolver`2.Resolve(ResolutionResult source) +153
   System.Linq.Enumerable.Aggregate(IEnumerable`1 source, TAccumulate seed, Func`3 func) +160
   AutoMapper.Mappers.PropertyMapMappingStrategy.MapPropertyValue(ResolutionContext context, IMappingEngineRunner mapper, Object mappedObject, PropertyMap propertyMap) +250

[AutoMapperMappingException: 

Mapping types:
Advert -> AdvertImageViewModel
MyProject.Models.Advert -> MyProject.ViewModels.AdvertImageViewModel

Destination path:
IList`1[1].AdvertImage

I can't figure out why this AdvertImage mapping is causing problems?


Solution

  • Change First to FirstOrDefault and it should work.

    Mapper.CreateMap<Advert, ListAdvertViewModel>()
                .ForMember(d => d.AdvertImage, o => o.MapFrom(s => (s.AdvertImages == null ? null : s.AdvertImages.FirstOrDefault())));