Bat*_*rog 5 automapper asp.net-mvc-3
我已经看到很多这样的错误发生的例子,因为各种各样的原因我已经看到了我能看到的所有原因,但我仍然得到错误,所以我想知道是否有人可以提供一些关于这个的信息错误实际上意味着,所以我可以尝试找到原因.这是一些代码:
控制器:
[HttpPost]
public ActionResult Edit(ProfileViewModel model)
{
if (ModelState.IsValid)
{
var person = new UserAttribute();
person = Mapper.Map<ProfileViewModel, UserAttribute>(model);
db.UserAttribute.Add(person);
db.SaveChanges();
}
Run Code Online (Sandbox Code Playgroud)
查看模型
public class ProfileViewModel
{
[Display(Name = "First Name")]
[StringLength(20)]
[Required]
public string FirstName { get; set; }
[Display(Name = "Last Name")]
[StringLength(30)]
[Required]
public string LastName { get; set; }
[Display(Name = "Gender")]
[Required]
public string Gender { get; set; }
[Display(Name = "Date of Birth")]
[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}", ApplyFormatInEditMode = true)]
public DateTime DOB { get; set; }
[Display(Name = "Hair Color")]
public string HairColor { get; set; }
[Display(Name = "Eye Color")]
public string EyeColor { get; set; }
[Display(Name = "Body Type")]
public string Weight { get; set; }
[Display(Name = "Height")]
public string HeightFeet { get; set; }
public string HeightInches { get; set; }
public int UserId { get; set; }
public IEnumerable<SelectListItem> WeightList { get; set; }
public IEnumerable<SelectListItem> EyeColorList { get; set; }
public IEnumerable<SelectListItem> HairColorList { get; set; }
public IEnumerable<SelectListItem> HeightFeetList { get; set; }
public IEnumerable<SelectListItem> HeightInchesList { get; set; }
public IEnumerable<SelectListItem> GenderList { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
UserAttribute模型:
public int ProfileId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Gender { get; set; }
public System.DateTime DOB { get; set; }
public string HairColor { get; set; }
public string EyeColor { get; set; }
public string HeightFeet { get; set; }
public string Weight { get; set; }
public int UserId { get; set; }
public string HeightInches { get; set; }
Run Code Online (Sandbox Code Playgroud)
映射配置:
public class AutoMapperConfiguration
{
public static void Configure()
{
Mapper.Initialize(x => x.AddProfile<ViewToDomainMapProfile>());
Mapper.Initialize(x => x.AddProfile<DomainToViewMapProfile>());
}
}
public class ViewToDomainMapProfile : Profile
{
public override string ProfileName
{
get { return "ViewToDomainMapProfile"; }
}
protected override void Configure()
{
Mapper.CreateMap<ProfileViewModel, UserAttribute>()
.ForSourceMember(x => x.GenderList, y => y.Ignore())
.ForSourceMember(x => x.HairColorList, y => y.Ignore())
.ForSourceMember(x => x.EyeColorList, y => y.Ignore())
.ForSourceMember(x => x.WeightList, y => y.Ignore())
.ForSourceMember(x => x.HeightFeetList, y => y.Ignore())
.ForSourceMember(x => x.HeightInchesList, y => y.Ignore());
}
}
Run Code Online (Sandbox Code Playgroud)
并在全局asax中调用配置:
AutoMapperConfiguration.Configure();
Run Code Online (Sandbox Code Playgroud)
Sun*_*nov 13
使用Mapper.AssertConfigurationIsValid();会产生以下异常:
AutoMapper.AutoMapperConfigurationException :
Unmapped members were found. Review the types and members below.
Add a custom mapping expression, ignore, add a custom resolver, or modify the source/destination type
==============================================================================================
ProfileViewModel -> UserAttribute (Destination member list)
----------------------------------------------------------------------------------------------
ProfileId
Run Code Online (Sandbox Code Playgroud)
所以,你需要添加映射ProfileId.
总的来说,Mapper.AssertConfigurationIsValid();在单元测试中使用它们(你拥有它们,对吗?),或者在映射器配置之后,这是一个很好的做法.它将显示此类错误配置的详细信息.
对于视图模型 => 用户属性
我注意到 ProfileId 是目标属性,但不是源属性。
public int ProfileId { get; set; }
Run Code Online (Sandbox Code Playgroud)
您是否需要添加代码来忽略此目标成员?
其他:我可能还建议使用或自定义自动映射器来映射仅按名称匹配的属性。
另外,如果可能,请避免模型名称以“属性”一词结尾,因为按照惯例,这几乎专门用于实际属性。(我为挑剔道歉)
| 归档时间: |
|
| 查看次数: |
9153 次 |
| 最近记录: |