0
votes

I am creating POC using Asp.Net Web API. For mapping one object type to another i am using AutoMapper(v5.1.1). Here are the types which is being used for mapping:

//Entity
public class Goal : IVersionedEntity
    {
        public virtual int GoalId { get; set; }
        public virtual string Title { get; set; }
        public virtual string Description { get; set; }
        public virtual DateTime StartDate { get; set; }
        public virtual DateTime EndDate { get; set; }
        public virtual string Reward { get; set; }
        public virtual DateTime? DisabledDate { get; set; }
        public virtual byte[] Version { get; set; }
        public virtual User User { get; set; }
        public virtual ICollection<Schedule> Schedules { get; set; }
    }
//Model
public class Goal
    {
        private List<Link> _links;
        public int GoalId { get; set; }
        public string Title { get; set; }
        public string Description { get; set; }
        public DateTime? StartDate { get; set; }
        public DateTime? EndDate { get; set; }
        //public Status Status { get; set; }
        public string Reward { get; set; }
        public DateTime? DisabledDate { get; set; }
        public User User { get; set; }
        public ICollection<Schedule> Schedules { get; set; }
        public List<Link> Links
        {
            get { return _links ?? (_links = new List<Link>()); }
            set { _links = value; }
        }
        public void AddLink(Link link)
        {
            _links.Add(link);
        }

    }

I am mapping Goal Entity to Goal model type object as following:

public async System.Threading.Tasks.Task Configure()
        {
            Mapper.Initialize(cfg => cfg.CreateMap<Data.Entities.Goal, Models.Goal>()
            .ForMember(m => m.Links, i => i.Ignore()));
        }

and here is the 'AutoMapperConfigurator' class in 'App_Start':

public void Configure(IEnumerable<IAutoMapperTypeConfigurator> autoMapperTypeConfigurations)
        {
            autoMapperTypeConfigurations.ToList().ForEach(m => m.Configure());
            Mapper.AssertConfigurationIsValid();
        }

But it is throwing following exception:

The following property on TestApp.Web.Api.Models.Goal cannot be mapped: Add a custom mapping expression, ignore, add a custom resolver, or modify the destination type TestApp.Web.Api.Models.Goal. Context: Mapping from type TestApp.Data.Entities.Goal to TestApp.Web.Api.Models.Goal Exception of type 'AutoMapper.AutoMapperConfigurationException' was thrown.

See it's not showing which property is not getting mapped. Any help for this isssue.

1

1 Answers

-1
votes

After spending hours on this, my final findings are follows:

  1. You must have all your entity models and service models mapping correct to make it work. Even if one fails, the mentioned exception will be thrown. And if your complex type mappings are not correct you will get the above error.
  2. In my case, I was missing how to configure the Complex Type with AutoMapper.
  3. To configure Complex Type, either add .ForMember(m => m.Property, i => i.Ignore()) to ignore the complex type mapping if not needed or .ForMember(m => m.Property, i => i.MapFrom(j => Mapper.Map<Entity,ServiceModel>(j.Property))) for nested mapping (refer: http://www.softwarerockstar.com/2011/05/complex-object-mapping-using-automapper/) or use CustomMapping if there is come specific requirement during the mapping