2
votes

I'm trying to map my DTO class to the native one. Here are the classes:

public class CategoryResource
    {
        public int Id { get; set; }
        public string Code { get; set; }
        public string Description { get; set; }
    }

public class Category
    {
        public int Id { get; set; }
        [Required]
        [StringLength(255)]
        public string Code { get; set; }
        [StringLength(255)]
        public string Description { get; set; }
        public ICollection<CategoryToProduct> Products { get; set; }

        public Category()
        {
            Products = new Collection<CategoryToProduct>();
        }
    }

and the usage

var category = mapper.Map<CategoryResource, Category>(categoryResource);

As above code throws an error of this kind:

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 =========================================================================== CategoryResource -> Category (Destination member list)

Unmapped properties: Products

I changed my mapping profile to:

CreateMap<CategoryResource, Category>()
                .ForMember(x=>x.Products, opt => opt.Ignore());

But still, I have the same error. Could you please advice me what I do wrong here? I already restarted the IIS and AutoMapper version is 6.2.2


To answer question from comments, that's the whole MappingProfile

public class MappingProfile : Profile
    {
        public MappingProfile()
        {
            //Domain to API
            CreateMap<Type, TypeResource>();
            CreateMap<Unit, UnitResource>();
            CreateMap<Category, CategoryResource>();


            //API to domain
            CreateMap<TypeResource, Type>();
            CreateMap<UnitResource, Unit>();

            CreateMap<CategoryResource, Category>()
                .ForMember(x=>x.Products, opt => opt.Ignore());
        }
    }
1
Remove the properties one by one to try and narrow down which is causing the issue.mjwills
Would you share "CreateMap" initialization ?lucky
You mean whole Mapping profile? I updated the question with thiswmioduszewski

1 Answers

5
votes

As of 6.2, AutoMapper creates type maps on the fly (documented here):

When you call Mapper.Map for the first time, AutoMapper will create the type map configuration and compile the mapping plan.

Given that the only thing MappingProfile is doing in your example code is ignoring the Products property, this leads me to believe that MapperProfile is not being registered.

See Integrating AutoMapper with ASP.NET Core DI by Jimmy Bogard for more details on how to register your custom Profiles. To summarise, you can use the AutoMapper.Extensions.Microsoft.DependencyInjection nuget package and either use the defaults or provide your own list of assemblies that need to be scanned.