Reputation: 1302
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.
Upvotes: 3
Views: 1502
Reputation: 3516
Mapper.Initialize can only be called once, when your app initializes itself, not per request as you're doing now.
Upvotes: 1