无法在AutoMapper 5中从ViewModel映射到ApplicationUser

时间:2016-09-17 00:04:23

标签: c# asp.net-mvc inheritance automapper automapper-5

我有一个继承自ApplicationUser基类(ASP.NET Identity)的Student类,它有一个名为StudentViewModel的ViewModel,如下所示:

实体类:

public class ApplicationUser : IdentityUser<int, ApplicationUserLogin,
                                     ApplicationUserRole, ApplicationUserClaim>, IUser<int>
{
    public string Name { get; set; }
    public string Surname { get; set; } 
    //code omitted for brevity
}

public class Student: ApplicationUser
{     
    public int? Number { get; set; }
}

视图模型:

public class StudentViewModel
{
    public int Id { get; set; }     
    public int? Number { get; set; }
    //code omitted for brevity
}

我使用以下方法通过将StudentViewModel映射到Controller中的ApplicationUser来更新Student:

[HttpPost]
[ValidateAntiForgeryToken]
public JsonResult Update([Bind(Exclude = null)] StudentViewModel model)
{
    //Mapping StudentViewModel to ApplicationUser ::::::::::::::::
    var student = (Object)null;

    Mapper.Initialize(cfg =>
    {
        cfg.CreateMap<StudentViewModel, Student>()
            .ForMember(dest => dest.Id, opt => opt.Ignore())
            .ForAllOtherMembers(opts => opts.Ignore());
    });

    Mapper.AssertConfigurationIsValid();
    student = Mapper.Map<Student>(model);
    //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

    //Then I want to pass the mapped property to the UserManager's Update method:
    var result = UserManager.Update(student);

    //code omitted for brevity              
}

使用此方法时,我遇到错误:&#34;方法的类型参数&#39; UserManagerExtensions.Update(UserManager,TUser)&#39;无法从使用中推断出来。尝试明确指定类型参数。&#34; 有什么想法来修复它吗?

0 个答案:

没有答案