文章
public class Article
{
public int Id { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public List<ArticleClassArticle> ArticleClassArticles { get; set; } = new();
}
文章分类
public class ArticleClass
{
public int Id { get; set; }
public string Name { get; set; }
public List<ArticleClassArticle> ArticleClassArticles { get; set; } = new();
}
文章和分类的多对多关系
public class ArticleClassArticle
{
/// <summary> 文章id </summary>
public int ArticleId { get; set; }
/// <summary> 文章 </summary>
public Article Article { get; set; }
/// <summary> 文章分类id </summary>
public int ArticleClassId { get; set; }
/// <summary> 文章分类 </summary>
public ArticleClass ArticleClass { get; set; }
}
文章对外的model (DTO)
public class ArticleViewDTO
{
public int Id { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public List<ArticleClassViewDTO> Classes { get; set; } = new();
}
分类对外的model (DTO)
public class ArticleClassViewDTO
{
public int Id { get; set; }
public string Name { get; set; }
}
AutoMapper配置
public class MapperProfile : Profile
{
public MapperProfile()
{
CreateMap<ArticleClassArticle, ArticleClassViewDTO>()
.ForMember(v => v.Id, ol => ol.MapFrom(s => s.ArticleClass.Id))
.ForMember(v => v.Name, ol => ol.MapFrom(s => s.ArticleClass.Name));
CreateMap<Article, ArticleViewDTO>()
.ForMember(v => v.Classes, ol => ol.MapFrom(s => s.ArticleClassArticles));
}
}