2017-05-09 109 views
1

我正在与asp.net核心和身份的项目, 我试图创建IdentityUser子语句和其对应的DTO之间使用Automapper的映射配置 我有做过类似的配置与其他类,它工作正常,但IdentityUser子类,它的行为是不同的:AutoMapper - 无法映射IdentityUser子类和其相应的DTO

这里是我的IdentityUser subclasse:

public partial class Collaborateur : IdentityUser 
{ 
    public Collaborateur() : base() 
    { 
     this.Activites = new HashSet<ActiviteCollaborateur>(); 
     this.ActeursAvantVente = new HashSet<ActeurAvv>(); 
    } 

    public string Nom { get; set; } 
    public string Prenom { get; set; } 
    public string Telephone { get; set; } 
    public Nullable<long> Matricule { get; set; } 
    public string Structure { get; set; } 
    public string Login { get; set; } 
    public RoleEnum Role { get; set; } 
    public virtual ICollection<ActiviteCollaborateur> Activites { get; set; } 
    public virtual ICollection<ActeurAvv> ActeursAvantVente { get; set; } 
    public virtual Agence Agence { get; set; } 
    public DateTime CreatedAt { get; set; } 
    public DateTime LastModified { get; set; } 
} 

其对应的DTO:

public class CollaborateurDTO : BaseDTO 
{ 
    public string Nom { get; set; } 
    public string Prenom { get; set; } 
    public string Telephone { get; set; } 
    public Nullable<long> Matricule { get; set; } 
    public string Structure { get; set; } 
    public string Login { get; set; } 
    public RoleEnum Role { get; set; } 
} 

CollaborateurProfile配置类:

public class CollaborateurProfile : Profile 
{ 
    CollaborateurProfile() 
    { 
     CreateMap<Collaborateur, CollaborateurDTO>().ReverseMap(); 
     CreateMap<Collaborateur, Collaborateur>() 
      .ForMember(x => x.Id, opt => opt.Ignore()) 
      .ForMember(x => x.CreatedAt, opt => opt.Ignore()) 
      .ForMember(x => x.LastModified, opts => opts.MapFrom(src => DateTime.UtcNow)); 
    } 
} 

和Startup.cs:

services.AddAutoMapper();

它停止在与

MissingMethodException这条线是由用户代码 的例外,未处理的在System.Private.Cor中发生类型'System.MissingMethodException' eLib.ni.dll但在用户代码中没有处理

+0

您可以尝试从您的项目中删除bin和obj文件夹,重新编译该项目并再次运行?请参阅http://stackoverflow.com/questions/8058832/system-missingmethodexception-method-not-found – Ignas

回答

0

到错误回答我在评论(https://stackoverflow.com/a/46567611/7131186

下面链接的问题,这个问题我的答案:

在我的情况(和它似乎也是你的情况)这是一个复制/粘贴问题。我莫名其妙地结束了一个私有的构造我的映射分布:

using AutoMapper; 

namespace Your.Namespace 
{ 
    public class MappingProfile : Profile 
    { 
     MappingProfile() 
     { 
      CreateMap<Animal, AnimalDto>(); 
     } 
    } 
} 

(注意失踪“公众”的构造函数的前面)

它编译完全正常,但是当AutoMapper尝试实例它不能(当然!)的配置文件找到构造函数!