3

I am trying to write mapping configuration for next case. I have domain object:

public class Category
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string ImagePath { get; set; }

    public virtual ICollection<Service> Services { get; set; }
    public int? SubcategoryId { get; set; }
    [ForeignKey("SubcategoryId")]
    public virtual Category Subcategory { get; set; }
}

And Dto to map:

public class CategoryDto
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string ImagePath { get; set; }
}

The problem is, target class have less properties, than source. If I use simple map, I get an exception.

Mapper.Initialize(n => n.CreateMap<Service, ServiceDto>());

I can't use Ignore(), because it will be applied to target class, not source one. Method ForSourceMember() also didn't help for some reason. I read this question, it's fine for most cases, but property Services is not null, it's Count = 0, when it's empty. I also read some similar questions from the right, but they didn't help, maybe they worked in previous versions.

Hope someone can help me to find solution, or explain what I missed.

QuarK
  • 1,162
  • 2
  • 12
  • 27
  • There is no problem with your map, actually that's the idea, you can have less things on your destination. So I don't know what error you see, but as far as AM is concerned, your map is good. – Lucian Bargaoanu Aug 27 '17 at 04:55
  • @LucianBargaoanu The exception is `AutoMapperMappingException` with message: Error mapping types. Mapping types: `IEnumerable`1 -> List`1 System.Collections.Generic.IEnumerable`1 [[DomainEntities.Category]] -> System.Collections.Generic.List`1 [[Web.Dtos.CategoryDto]]`. The inner exception is Missing type map configuration or unsupported mapping. Mapping types: `Category -> CategoryDto, DomainEntities.Category -> Web.Dtos.CategoryDto`. Exception thrown in `Mapper.Map, List>(_unitOfWork.Categories.GetAll())`. Usually this means he can't find pair for some prop – QuarK Aug 27 '17 at 14:10
  • A repro would help. Make a [gist](https://gist.github.com/lbargaoanu/9c7233441c3a3413cc2b9b9ebb5964a9) that we can execute and see fail. – Lucian Bargaoanu Aug 27 '17 at 15:42
  • @LucianBargaoanu Hope I understand you right, here is [repro](https://gist.github.com/QuarK264/f496efa0441a7dd9104911e6de9fe65d). This code works fine, strange. They both do basically the same thing. I add `ToList()` to problem code, but nothing changed. I write bit more [full version](https://gist.github.com/QuarK264/932b2d8833ee44d906f900d1a5595115), maybe you can notice the difference. Everything is fine on line 13, but crushes on next line. – QuarK Aug 27 '17 at 21:24

1 Answers1

1

Mapper.Initialize can only be called once, when your app initializes itself, not per request as you're doing now.

Lucian Bargaoanu
  • 3,336
  • 3
  • 14
  • 19
  • So mapper only remembers last map for `Orders`, doesn't it? Is there any ways to have more than one map config? Will your gist example from second comment fit? – QuarK Aug 28 '17 at 13:12
  • 1
    Yes. Inside that one Initialize call, you can have many CreateMap-s. Or use profiles. The [docs](https://github.com/AutoMapper/AutoMapper/wiki/Configuration). – Lucian Bargaoanu Aug 28 '17 at 13:16
  • 1
    Christ, just realized AutoMapper developer helped me with my stupid question. I am not even a junior, I am not worthy! Thank You a lot. – QuarK Aug 29 '17 at 15:20