Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Nadia 45 posts 122 karma points
    Jun 14, 2019 @ 02:22
    Nadia
    0

    Custom Automapper profile error

    Hey, I have setup a custom Automapper within my Umbraco solution. But I am getting the following error

    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 For no matching constructor, add a no-arg ctor, add optional arguments, or map all of the constructor parameters ========================================================== AutoMapper created this type map for you, but your types cannot be mapped using the current configuration. ContactViewModel -> ContactDto (Destination member list) Web.Models.ContactViewModel -> DTO.Models.POCO.ContactDto (Destination member list)

    Unmapped properties: Id BrowserVersion IpAddress CreatedOn Description: An unhandled exc

    These feilds get set later, not on the mapper. I have tried telling the mapper profile to ignore the fields, and from what I can find on google it is related to the automapper configuration. But I can't see what I have been doing wrong in the umbraco setup

    I have setup the following profile

     public class MapperComposer : IComposer
    {
        public void Compose(Composition composition)
        {
            composition.Register<Profile, CustomMapperProfile>();
        }
    }
    internal class CustomMapperProfile :Profile
    {
        public CustomMapperProfile()
        {
    
           AllowNullDestinationValues = true;
    
            CreateMap<ContactViewModel, ContactDto>()
                .ForMember(x => x.BrowserVersion, opt => opt.Ignore())
                .ForMember(x => x.IpAddress, opt => opt.Ignore())
                .ForMember(x => x.Id, opt => opt.Ignore())
                .ForMember(x => x.CreatedOn, opt => opt.Ignore())
    
            ;
    
        }
    }
    

    Mapping in my surface controller looks like

     ContactDto dto = Mapper.Map<ContactDto>(model);
    

    and my 2 models

    [TableName("NV_Contact")]
    [PrimaryKey("Id", AutoIncrement = true)]
    [ExplicitColumns]
    public class ContactDto : IContactDto
    {
        [NullSetting(NullSetting = NullSettings.NotNull)]
    
        public int Id { get; set; }
        [NullSetting(NullSetting = NullSettings.Null)]
        public string Name { get; set; }
        public string Email { get; set; }
        [NullSetting(NullSetting = NullSettings.Null)]
        public string Address { get; set; }
        [NullSetting(NullSetting = NullSettings.Null)]
        public string PhoneNumber { get; set; }
        [NullSetting(NullSetting = NullSettings.Null)]
        public string Message { get; set; }
    
        [NullSetting(NullSetting = NullSettings.Null)]
        public string BrowserVersion { get; set; }
        [NullSetting(NullSetting = NullSettings.Null)]
        public string IpAddress { get; set; }
    
        [NullSetting(NullSetting = NullSettings.NotNull)]
        public DateTime CreatedOn { get; set; }
    }
    
    
     public class ContactViewModel
    {
        [Required(ErrorMessage = "Please enter your name")]
        [StringLength(100, ErrorMessage = "Name is limited to 100 characters")]
        [Display(Name="Name")]
        public string Name { get; set; }
    
        [Required(ErrorMessage = "Please enter your email address")]
        [EmailAddress(ErrorMessage = "Invalid email address")]
        [StringLength(100, ErrorMessage = "Email is limited to 100 characters")]
        [Display(Name = "Email")]
        public string Email { get; set; }
    
        [StringLength(20, ErrorMessage = "Phone Number is limited to 20 characters")]
        [Display(Name = "Phone Number")]
        public string PhoneNumber { get; set; }
    
        [StringLength(200, ErrorMessage = "Address is limited to 200 characters")]
        [Display(Name = "Address")]
        public string Address { get; set; }
    
        [Required(ErrorMessage = "Please enter a message")]
        [StringLength(2000, ErrorMessage = "Message is limited to 2000 characters")]
        [Display(Name = "Message")]
        public string Message { get; set; }
    
    
        //Email details
    
        public string EmailSubject { get; set; }
    
        public string ElementKey { get; set; }
    
    
    }
    
  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies