Ignore navigation property when matching in Automapper
Having these 2 classes as an example:
public class Product
{
public int Id {get; set;}
public int CategoryId {get; set;}
}
public class ProductDTO
{
public int Id {get; set;}
public int CategoryId {get; set;}
public Category Category {get; set;}
}
How can I ignore ProductDTO.Category
when matching bidrectionally?
+3
source to share
1 answer
Assuming you mean bidirectional, that is, both classes have a member Category
that you want to ignore, you can use .ReverseMap()
.
<strong> Display
Mapper.CreateMap<Product, ProductDTO>()
.ForMember(dest => dest.Category, opt => opt.Ignore()).ReverseMap();
Model examples
public class Product
{
public int Id {get; set;}
public int CategoryId {get; set;}
public Category Category {get; set;}
}
public class ProductDTO
{
public int Id {get; set;}
public int CategoryId {get; set;}
public Category Category {get; set;}
}
public class Category
{
}
+2
source to share