Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to debug AutoMapper "Missing type map configuration or unsupported mapping" error

I have seen plenty of examples of this error occuring, for a wide variety of causes and I have gone through all the causes I can see, but still i get the error, so I am wondering if some one can give some information about what this error actually means, so i can try finding the cause. Here is some code:

Controller:

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

        }

View Model

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 model:

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

Mapping config:

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

and the config is called in the global asax:

        AutoMapperConfiguration.Configure();
like image 855
BattlFrog Avatar asked Jun 09 '13 01:06

BattlFrog


1 Answers

Using Mapper.AssertConfigurationIsValid(); produces the following exception:

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

So, you need to add mapping for ProfileId.

Overall, it's a good practice to use Mapper.AssertConfigurationIsValid(); either in your unit tests (you have them, right?), or after your mapper configuration. It'll display detailed information for such a misconfigurations.

like image 161
Sunny Milenov Avatar answered May 16 '23 01:05

Sunny Milenov