1

I am trying to use AutoMapper to map a ViewModel to a Model.

Here is my simplified ViewModel (the source) class:

    public class EditPaypointVM
    {

        public Int64 Id { get; set; }

        public Int64 OrganisationId { get; set; }

        [Required]
        public string OrganisationContactNumber { get; set; }

        public Int64 PostalAddressId { get; set; }

        public string PostalAddressAddressText { get; set; }

        [Required]
        public Int64 PostalAddressArea { get; set; }

        public string PostalAddressAreaText { get; set; }

        public string PostalAddressCode { get; set; } 

        public Int64 PhysicalAddressId { get; set; }

        public string PhysicalAddressAddressText { get; set; }

        [Required]
        public Int64 PhysicalAddressArea { get; set; }

        public string PhysicalAddressAreaText { get; set; }

        public string PhysicalAddressCode { get; set; }
    }

Here is my simplified Model (destination) class:

    public class Paypoint
    {
        public Int64 Id { get; set; }

        public virtual Organisation Organisation { get; set; }

        public virtual Employer Employer { get; set; }

        public virtual List<EmploymentContract> EmploymentContracts { get; set; }

        public bool IsActive { get; set; }
    }


    public class Organisation
    {
        public Int64 Id { get; set; }

        public virtual List<EmailAddress> EmailAdresses { get; set; }

        public virtual List<ContactNumber> ContactNumbers { get; set; }

        public virtual List<Address> Adresses { get; set; }

        public string RegisteredName { get; set; }

        public string TradingName { get; set; }

        public string RegistrationNumber { get; set; }

        public string WebsiteAddress { get; set; }
    }

Here is the code I execute to create mappings in memory on application start:

        Mapper.CreateMap<EditPaypointVM, Paypoint>()
           .ForMember(dest => dest.IsActive,
                opt => opt.UseValue(true))
           .ForMember(dest => dest.Organisation,
                opt => opt.UseDestinationValue())
           .Ignore(i => i.Employer)
           .Ignore(i => i.EmploymentContracts);

Upon executing 'AssertConfigurationIsvalid' within a unit test, an "Unmapped error is thrown which states that the Organisation member of Paypoint is unmapped.

Any ideas on what causes this?

CShark
  • 2,183
  • 1
  • 24
  • 42
  • What is the type of `Organisation`? By the way in order to the AssertConfigurationIsvalid work you need to Ignore your property with `.Ignore(i => i.Organisation)` – nemesv Oct 12 '14 at 13:38
  • Or you need to write: `.ForMember(dest => dest.Organisation, opt => { opt.MapFrom(src => src.Organisation); opt.UseDestinationValue(); });` to make the `AssertConfigurationIsvalid` happy – nemesv Oct 12 '14 at 13:53
  • @nemesv Organisation is a complex type which contains collections of other complex types like Addresses. Some of the internal properties of Organisation needs to be set and that is why I applied 'UseDestinationValue'. – CShark Oct 12 '14 at 15:11
  • @nemesv The source class, `EditPaypointVM` has been flattened and does not contain a single property which can be mapped to `Organisation`. Rather it just has properties which maps to attributes of organisation. So `.ForMember(dest => dest.Organisation, opt => { opt.MapFrom(src => src.Organisation); opt.UseDestinationValue(); });` does not compile. – CShark Oct 12 '14 at 15:22
  • The `.ForMember(dest => dest.Organisation, opt => { opt.MapFrom(src => src.Organisation); opt.UseDestinationValue(); });` should compile... what is the error message? Which version of Automapper are you using? – nemesv Oct 12 '14 at 15:26
  • The error is "'EditPaypointVM' does not contain a definition for 'Organisation'"... I am using version 3.2.1 – CShark Oct 12 '14 at 15:33
  • @nemesv I added the code for the two classes to more clearly illustrate why this code throws an error. Why is it necessary to call `.Ignore(i => i.Organisation)` in order for `AssertConfigurationIsvalid` to work? Is there a known issue with `UseDestinationValue`? I could not find anything in this regard on their github page. – CShark Oct 13 '14 at 04:49

0 Answers0