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

2023-12-10

我有一个从 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 映射到控制器中的 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              
}

在使用这个方法的时候,我遇到了一个错误:“无法从用法中推断出方法‘UserManagerExtensions.Update(UserManager, TUser)’的类型参数。尝试显式指定类型参数。”有办法解决吗?


None

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

无法在 AutoMapper 5 中从 ViewModel 映射到 ApplicationUser 的相关文章

随机推荐