【发布时间】:2017-10-29 20:15:56
【问题描述】:
我有两个类 User (Destination) 和 UserViewModel (Source) :
public class User
{
public int Id{ get; set; }
public string Username{ get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public DateTime? DateOfBirth { get; set; }
public Enums.Sex Sex { get; set; }
public byte[] ProfilePicture { get; set; }
public int Score { get; set; }
}
public class ProfileViewModel
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string PhoneNumber { get; set; }
public string Email { get; set; }
}
这是 AutoMapper 配置:
Mapper.Initialize(cfg =>
{
cfg.CreateMap<ProfileViewModel, User>()
.ForMember<int>(u =>u.Id, m => m.Ignore()).ForMember<string>(u=> u.UserName, m => m.Ignore());
});
我在这里使用它:
public ActionResult MyProfile(ProfileViewModel m, HttpPostedFileBase profilePicture)
{
User currentUser = UserRep.GetCurrentUser(User.Identity.GetUserId());
currentUser = Mapper.Map<User>(m);
...
}
我为当前用户带来了所有数据 id、用户名......当我执行映射时,用户名是 null 并且 id = 0 我不明白,我尝试使用 ignore() 和 usedestinationValue()
【问题讨论】:
标签: c# asp.net automapper