在MVC应用程序中,有来自ApplicationUser
基类继承一个学生类(ASP.NET身份),并有一种叫StudentViewModel
一个ViewModel
如下图所示:为什么Automapper不工作的基础和继承类
实体类:
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
:
[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
}
使用此方法时,我会遇到一个错误:
The type arguments for method 'UserManagerExtensions.Update(UserManager, TUser)' cannot be inferred from the usage. Try specifying the type arguments explicitly.
任何主意修理它?
@BalagurunathanMarimuthu你有什么想法? –