asp.net-mvc-3 – 如何调试AutoMapper“缺少类型映射配置或不支
发布时间:2020-12-16 03:31:05 所属栏目:asp.Net 来源:网络整理
导读:我已经看到很多这样的错误发生的例子,因为各种各样的原因我已经看到了我能看到的所有原因,但我仍然得到错误,所以我想知道是否有人可以提供一些关于这个的信息错误实际上意味着,所以我可以尝试找到原因.这是一些代码: 控制器: [HttpPost] public ActionResu
|
我已经看到很多这样的错误发生的例子,因为各种各样的原因我已经看到了我能看到的所有原因,但我仍然得到错误,所以我想知道是否有人可以提供一些关于这个的信息错误实际上意味着,所以我可以尝试找到原因.这是一些代码:
控制器: [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();
}
查看模型 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; }
}
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; }
映射配置: 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());
}
}
并在全局asax中调用配置: AutoMapperConfiguration.Configure(); 解决方法
使用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 因此,您需要为ProfileId添加映射. 总的来说,使用Mapper.AssertConfigurationIsValid()是一个好习惯.要么在单元测试中(你拥有它们,对吗?),还是在映射器配置之后.它将显示此类错误配置的详细信息. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net – Web.Config无法呈现特殊字符
- strings
- asp.net – 如何从JavaScript访问ASPxTextBox的值
- asp.net – IIS Web服务器中的此错误(扩展配置)是什么?
- asp.net – 在RedirectToAction调用中传播QueryString参数
- asp.net-mvc-4 – 如何在MVC 4中创建自定义WebSecurity.Log
- asp.net-mvc – 如何在Microsoft.AspNet.Mvc.Facebook.Face
- asp.net-mvc – 无论如何让mvc框架验证我的action参数,就像
- asp.net-mvc – 动态生成sitemap.xml
- asp.net – 支持部署到Windows Azure的论坛软件
推荐文章
站长推荐
- 在ASP.NET中单点登录 – cookie名称,machineKey还
- 基于Azure IoT开发.NET物联网应用系列-全新的Azu
- asp.net – MVC 6 WebAPI返回序列化的HttpRespon
- asp.net – ELMAH – 过滤404错误
- ASP.NET core MVC动作过滤器执行顺序
- ASP.net RequiredFieldValidator VisualStudio 2
- 在ASP.NET菜单控件中设置item.selected
- ASP.NET Core 认证与授权[2]:Cookie认证
- unit-testing – 单元测试自定义验证过滤器
- asp.net – 将我的Web应用程序文件发布到远程服务
热点阅读
