首页 > 解决方案 > 使用 AutoMapper 在 Entity Framework Core 中进行多对一映射

问题描述

我有两个模型类来获取包含全名的评论列表:

public partial class Comment
{
    public int Id { get; set; }
    public string Comment{ get; set; }
    public int? AddBy{ get; set; }
    public virtual User { get; set; }
}

public partial class User
{
    public int Id { get; set; }
    public string FullName { get; set; }
    public virtual ICollection<Comment> Comments{ get; set; }
}

这是我的评论 Dto

public partial class CommentDto
{
    public int Id { get; set; }
    public string Comment { get; set; }
    public string FullName { get; set; }
}

我的查询选择了所有带有用户(全名)的评论,但包含在未给出用户模型的映射器中

CreateMap<Comment, CommentDto>()
            .ForMember(x => x.FullName, opt => opt.MapFrom(x => x

标签: entity-framework-coreasp.net-core-mvcautomapper

解决方案


推荐阅读