I got two tables with many to many relationship and I don’t know how to map it. This is the BlogPost
model class:
public class BlogPost
{
public Guid Id { get; set; }
public string Title { get; set; }
public string ShortDescription { get; set; }
public string Content { get; set; }
public string FeaturedImageUrl { get; set; }
public string UrlHandle { get; set; }
public DateTime PublishedDate { get; set; }
public string Author { get; set; }
public bool IsVisible { get; set; }
public ICollection<Category> Categories { get; set; }
}
And this is Category
:
public class Category
{
public Guid Id { get; set; }
public string Name { get; set; }
public string UrlHandle { get; set; }
public ICollection<BlogPost> BlogPosts { get; set;}
}
This is my DTO to create a BlogPost
:
public class CreateBlogPostRequestDTO
{
public string Title { get; set; }
public string ShortDescription { get; set; }
public string Content { get; set; }
public string FeaturedImageUrl { get; set; }
public string UrlHandle { get; set; }
public DateTime PublishedDate { get; set; }
public string Author { get; set; }
public bool IsVisible { get; set; }
public Guid[] Categories { get; set; }
}
This is my mapper and it is not working
public class AutoMapperProfile : Profile
{
public AutoMapperProfile()
{
//category
CreateMap<CreateCategoryRequestDTO, Category>().ReverseMap();
CreateMap<UpdateCategoryRequestDTO, Category>().ReverseMap();
CreateMap<Category, CategoryDTO>().ReverseMap();
//blogpost
//have to custom here but i dont know how to do it
CreateMap<CreateBlogPostRequestDTO, BlogPost>().ReverseMap();
CreateMap<BlogPost, BlogPostDTO>().ReverseMap();
}
}
I’m using AutoMapper, I know I have to customize the mapper