【问题标题】:How to debug AutoMapper "Missing type map configuration or unsupported mapping" error如何调试 AutoMapper “缺少类型映射配置或不支持的映射”错误
【发布时间】:2013-06-09 01:38:31
【问题描述】:

我已经看到很多发生此错误的示例,原因多种多样,我已经检查了所有我能看到的原因,但我仍然收到错误,所以我想知道是否有人可以提供一些信息关于这个错误的实际含义,所以我可以尝试找出原因。这是一些代码:

控制器:

    [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; }

}

用户属性模型:

    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();

【问题讨论】:

  • 这是编译错误还是运行时错误?此外,域到视图模型的映射是否正常工作?

标签: asp.net-mvc-3 automapper


【解决方案1】:

使用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(); 是一种很好的做法(您拥有它们,对吗?),或者在您的映射器配置之后。它将显示此类错误配置的详细信息。

【讨论】:

  • 这救了我。只需添加 Mapper.AssertConfigurationIsValid();在我的 global.asax 映射中,让我看到我的映射使用的是同名的不同类。 (忽略这不是一个很好的做法,它是遗留代码)我没有意识到如果 Mapper.Map() 找不到所有成员的映射,它不会抛出错误。它只是在尝试映射时失败。
【解决方案2】:

对于视图模型 => 用户属性

我注意到 ProfileId 是目标属性,而不是源属性。

public int ProfileId { get; set; }

您需要添加代码来导入此目标成员吗?

其他: 我可能还建议使用或自定义自动映射器来映射仅按名称匹配的属性。

此外,如果可能,请避免模型名称以“属性”一词结尾,因为按照惯例,这几乎专门用于实际属性。 (我很抱歉吹毛求疵)

【讨论】:

    猜你喜欢
    • 2013-01-18
    • 2019-03-01
    • 2017-04-16
    • 2015-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-15
    • 2015-05-07
    相关资源
    最近更新 更多