using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
public static void Main()
var config = new MapperConfiguration(cfg =>
cfg.CreateMap<Brand, string>()
.ConvertUsing(source => source.Name ?? string.Empty);
cfg.CreateMap<string, Brand>()
.ConvertUsing(source => new Brand { Name = source });
cfg.CreateMap<Product, ProductDto>()
.ForMember(dest => dest.Brand, opt => opt.MapFrom(p => p.Brand.Name))
.ForMember(dest => dest.Categories, opt => opt.MapFrom(p => p.ProductCategories.Select(pc => pc.Category.Name)))
IMapper mapper = config.CreateMapper();
var product = new Product
Description = "Product 1 Desc",
Brand = new Brand { Id = 1, Name = "Brand 1" },
ProductCategories = new List<ProductCategory>
new ProductCategory { Category = new Category { Name = "Category 1" } }
var productDto = mapper.Map<ProductDto>(product);
var reverseProduct = mapper.Map<Product>(productDto);
Console.WriteLine(JsonConvert.SerializeObject(reverseProduct));
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public decimal Price { get; set; }
public int BrandId { get; set; }
public Brand Brand { get; set; }
public ICollection<ProductCategory> ProductCategories { get; set; }
public int Id { get; set; }
public string Name { get; set; }
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public decimal Price { get; set; }
public string Brand { get; set; }
public List<string> Categories { get; set; }
public class ProductCategory
public Category Category { get; set; }
public string Name { get; set; }