当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - Automapper 5.2 with inheritance not working

问题描述:

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.

网友答案:

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.

分享给朋友:
您可能感兴趣的文章:
随机阅读: