在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你有什麼想法? –