2017-01-28 83 views
0

我有一个实体:Automapper 5.2继承不工作

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; } 
} 

而且我有一个视图模型:

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; } 
} 

我需要Entity.Certificate映射到ViewModel.Certificate(我不真的需要映射BaseEntity,我想摆脱它)。 我有映射简介:

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>(); 
    } 
} 

在ConfigureServices注册:

services.AddAutoMapper(); 

所有上述3类不同的项目中(如果它使任何SENCE)。

这是控制器的构造函数:

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; 
    } 

而且GET方法,我映射实体视图模型:

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); 

看起来是正确的,对不对? 但是:

错误映射类型。

映射类型: IEnumerable的 System.Collections.Generic.IEnumerable 1[[AlexanderTsema.Storage.Entities.Entities.Certificate, AlexanderTsema.Storage.Entities, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] -> System.Collections.Generic.IEnumerable 1 [[AlexanderTsema.ViewModels.ViewModels.Certificate,AlexanderTsema.ViewModels,版本= 1.0.0.0,文化=中性公钥=空]]

和Inner Exc: 缺少类型映射配置或不支持的映射。

映射类型: 证书 - >证书 AlexanderTsema.Storage.Entities.Entities.Certificate - > AlexanderTsema.ViewModels.ViewModels.Certificate

请指点。

我试图将Entity.Certificate映射到Entity.Certificate,它工作正常。

+0

在发布quiestion之前阅读帮助中心文章http://stackoverflow.com/help/tagging特别是**我应该在标题中使用标签吗?**部分:_避免将标签插入以下任何格式的标题中: [标签]:[问题标题] _ – Tseng

+0

@ user2970104快速的问题,但。如果您从中删除所有参数,智能感知会向您显示“AddAutoMapper”的什么签名?您使用的是哪个平台,完整的.NET Framework还是.NET Core应用程序?我试图理解为什么调用它没有参数不起作用。 –

+0

bot编辑问题标题:(这是ASP.NET Core 1.1。Intellisense没有显示任何具体内容。@MickaëlDerriey – user2970104

回答

0

我想你可能会缺少的是AutoMapper可以用来发现配置文件的至少一种类型或程序集。

services.AddAutoMapper(typeof(MappingProfile));应该这样做。