AutoMapper - 无法在IdentityUser子类及其对应的DTO之间进行映射

时间:2017-05-09 13:59:00

标签: asp.net asp.net-core asp.net-identity automapper

我正在开发一个带有asp.net核心和身份的项目, 我正在尝试使用Automapper在IdentityUser子类及其对应的DTO之间创建映射配置 我已经完成了与其他类相似的配置,并且工作正常,但对于IdentityUser子类,它的行为有所不同:

这是我的IdentityUser子类:

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.CoreLib.ni.dll中出现“System.MissingMethodException”类型的异常,但未在用户代码中处理

1 个答案:

答案 0 :(得分:0)

我错误地在评论中链接的问题(https://stackoverflow.com/a/46567611/7131186

回答了这个问题

以下是我的回答:

在我的情况下(似乎这也是你的情况)这是一个复制/粘贴问题。我不知何故最终得到了我的映射配置文件的PRIVATE构造函数:

%default total

plus_assoc : (x : Nat) -> (y : Nat) -> (z : Nat) -> (x + y) + z = x + (y + z)

plus_comm : (x : Nat) -> (y : Nat) -> x + y = y + x

abcd_to_acbd_lemma : (a : Nat) -> (b : Nat) -> (c : Nat) -> (d : Nat) -> 
                (a + b) + (c + d) = (a + c) + (b + d)
abcd_to_acbd_lemma a b c d = 
    let e1 = the ((a + b) + (c + d) = ((a + b) + c) + d) $ sym (plus_assoc (a + b) c d)
        e2 = the (((a + b) + c) + d = (a + (b + c)) + d) $ rewrite (plus_assoc a b c) in Refl
        e3 = the ((a + (b + c)) + d = (a + (c + b)) + d) $ rewrite (plus_comm b c) in Refl
        e4 = the ((a + (c + b)) + d = ((a + c) + b) + d) $ rewrite (plus_assoc a c b) in Refl
        e5 = the ((((a + c) + b) + d) = (a + c) + (b + d)) $ plus_assoc (a + c) b d
    in trans e1 $ trans e2 $ trans e3 $ trans e4 e5

(注意在ctor前面缺少的“公众”)

编译完全正常的

,但是当AutoMapper尝试实例化它时,它当然不能(当然!)找到构造函数!