0
votes

I have an Entity:

public abstract class BaseEntity
{
     public Int16 Id { get; set; }
     public DateTime Timestamp { get; set; }
}

public class Certificate : BaseEntity
{
    public String Name { get; set; }
    public String Authority { get; set; }
    public DateTime RecieveDate { get; set; }
    public String Image { get; set; }
}

And I have a ViewModel:

public abstract class BaseViewModel
{
    public Int16 Id { get; set; }
    public DateTime Timestamp { get; set; }
}

public class Certificate : BaseViewModel
{
    public String Name { get; set; }
    public String Authority { get; set; }
    public DateTime RecieveDate { get; set; }
    public String Image { get; set; }
}

I need to map Entity.Certificate to ViewModel.Certificate (I don't really need to map BaseEntity at all, I want to get rid of it). I have Mapper profile:

 public class MappingProfile : Profile
 {
    public MappingProfile()
    {
        CreateMap<Storage.Entities.Entities.BaseEntity, ViewModels.ViewModels.BaseViewModel>()
            .Include<Storage.Entities.Entities.Certificate, ViewModels.ViewModels.Certificate>();
        CreateMap<Storage.Entities.Entities.Certificate, ViewModels.ViewModels.Certificate>();
    }
 }

Registered in ConfigureServices:

services.AddAutoMapper();

All 3 classes mentioned above are in different projects (if it makes any sence).

This is controller constructor:

    private readonly Storage.Abstractions.Core.IStorage _storage;
    private readonly IMapper _mapper;
    public CertificateController(Storage.Abstractions.Core.IStorage storage, IMapper mapper)
    {
        this._storage = storage;
        this._mapper = mapper;
    }

And Get Method where I map entity to viewModel:

   IEnumerable<Storage.Entities.Entities.Certificate> certificates = await this._storage.GetRepository<ICertificateRepository>().AllAsync();
   IEnumerable<ViewModels.ViewModels.Certificate>result = _mapper.Map<IEnumerable<Storage.Entities.Entities.Certificate>, IEnumerable<ViewModels.ViewModels.Certificate>>(certificates);

Looks correct, right? BUT:

Error mapping types.

Mapping types: IEnumerable1 -> IEnumerable1 System.Collections.Generic.IEnumerable1[[AlexanderTsema.Storage.Entities.Entities.Certificate, AlexanderTsema.Storage.Entities, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] -> System.Collections.Generic.IEnumerable1[[AlexanderTsema.ViewModels.ViewModels.Certificate, AlexanderTsema.ViewModels, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]

and Inner Exc: Missing type map configuration or unsupported mapping.

Mapping types: Certificate -> Certificate AlexanderTsema.Storage.Entities.Entities.Certificate -> AlexanderTsema.ViewModels.ViewModels.Certificate

Please advice.

I tried to map Entity.Certificate to Entity.Certificate and it works fine.

1
Read the help center articles before posting a quiestion stackoverflow.com/help/tagging especially the Should I use tags in titles? section: Avoid inserting tags into titles in any of the following formats: [tag]: [question title] - Tseng
@user2970104 Quick questions, though. What signature of AddAutoMapper does IntelliSense show you if you remove all arguments from it? What platform do you use, the full .NET Framework or is it a .NET Core application? I'm trying to understand why calling it with no parameters didn't work. - Mickaël Derriey
bot edits question title :( It's ASP.NET Core 1.1. Intellisense doesn't show anything specific. @MickaëlDerriey - user2970104

1 Answers

1
votes

I think what you might be missing is at least one type or assembly that AutoMapper could use to discover profiles.

services.AddAutoMapper(typeof(MappingProfile)); should do the trick.