【发布时间】:2016-06-10 17:27:33
【问题描述】:
我尝试了很多搜索,并尝试了不同的选项,但似乎没有任何效果。
我正在使用 ASP.net Identity 2.0 并且我有 UpdateProfileViewModel 。更新用户信息时,我想将 UpdateProfileViewModel 映射到 ApplicationUser(即身份模型);但我想保留这些值,我从用户的数据库中获得。即用户名和电子邮件地址,无需更改。
我试过了:
Mapper.CreateMap<UpdateProfileViewModel, ApplicationUser>()
.ForMember(dest => dest.Email, opt => opt.Ignore());
但映射后我仍然收到电子邮件为空:
var user = await UserManager.FindByIdAsync(User.Identity.GetUserId());
user = Mapper.Map<UpdateProfileViewModel, ApplicationUser>(model);
我也试过了,但是不行:
public static IMappingExpression<TSource, TDestination> IgnoreAllNonExisting<TSource, TDestination>(this IMappingExpression<TSource, TDestination> expression)
{
var sourceType = typeof(TSource);
var destinationType = typeof(TDestination);
var existingMaps = Mapper.GetAllTypeMaps().First(x => x.SourceType.Equals(sourceType) && x.DestinationType.Equals(destinationType));
foreach (var property in existingMaps.GetUnmappedPropertyNames())
{
expression.ForMember(property, opt => opt.Ignore());
}
return expression;
}
然后:
Mapper.CreateMap<UpdateProfileViewModel, ApplicationUser>()
.IgnoreAllNonExisting();
【问题讨论】:
-
尝试“UseDestinationValue”而不是“Ignore”
-
它在用户对象中仍然保持为空。
标签: asp.net asp.net-mvc-5 asp.net-identity automapper asp.net-identity-2