如何使用 AutoMapper EF Core 将多个相关实体映射到一个 DTO 对象

How Map Multiple related Entities to one DTO Object using AutoMapper EF Core

我的 blazor 应用程序中有三个相关实体 OpportunityAppUserAssignedOpportunity,我想要实现的是将 OpportunityAppUser 映射到具有相似字段的 DTO 对象 ReturnAssignedOpportunityDTO作为实体,使用 AutoMapper,但我不确定该怎么做,下面是实体

 public partial class AssignedOpportunity
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int ID { get; set; }

    [ForeignKey("OpportunityID")]
    public string OpportunityID { get; set; }
    public string Status { get; set; }
    public Opportunity opportunity { get; set; }


    [ForeignKey("UserID")]
    public string UserID { get; set; }
    public AppUser User { get; set; }
}

机会

 public partial class Opportunity
{
    public Opportunity()
    {           
        AssignedOpportunities= new HashSet<AssignedOpportunity>();
    }
    [Key]
    public string ID { get; set; }
    public string OpportunityName { get; set; }
    public string Description { get; set; }
    public string CreatedBy { get; set; }
    public DateTime DateCreated { get; set; }
    public double EstimatedValue { get; set; }
    public string EmployeeNeed { get; set; }
    public double RealValue { get; set; }
    public string Location { get; set; }
    public string ReasonStatus { get; set; }
    public string Status { get; set; }
     public virtual ICollection<AssignedOpportunity> AssignedOpportunities { get; set; }
}

AppUserClass

 public partial class AppUser : IdentityUser
{
    public AppUser()
    {
        AssignedOpportunities = new HashSet<AssignedOpportunity>();
    }
    public string FirstName { get; set; }
    public string MiddleName { get; set; }
    public string LastName { get; set; }
    public string Gender { get; set; }
    public string Street { get; set; }
    public string City { get; set; }
    public string LGA { get; set; }
    public string State { get; set; }
    public virtual ICollection<AssignedOpportunity> AssignedOpportunities { get; set; }

}

这是我要映射到的 DTO 对象。

 public class ReturnOpportunitiesDTO
{
    public int ID { get; set; }
    public string OpportunityID { get; set; }
    public string OpportunityName { get; set; }
    public double EstimatedValue { get; set; }
    public string EmployeeNeed { get; set; }
    public double RealValue { get; set; }
    public string Location { get; set; }
    public string UserID { get; set; }
    public string UserFullName { get; set; }      
    public string Status { get; set; }
}

这是我获取记录的查询

 var result = await _context.AssignedOpportunities.Include(o => o.opportunity).
                ThenInclude(a => a.User).
                Where(a=>a.UserID==UserID.ToString()).ToListAsync();
            return result;

这就是我通常设置地图配置文件的方式

 public AssignArtisanProfile()
    {
        CreateMap<AssignedOpportunity, ReturnOpportunities>();
    }

但是由于我想映射多个实体,我该如何包含另一个实体

您的方案只是展平复杂对象的另一个示例。您在子对象中具有属性,您希望将其带到底层,同时仍利用 AutoMapper 映射功能。如果您可以在从分配的机会映射到 DTO 时重用来自应用程序用户和机会的其他地图......好吧,有一个名为 IncludeMembers() 的方法(请参阅 docs)正是针对这种情况存在的.它允许您为子类型重用现有映射中的配置:

config.CreateMap<AssignedOpportunity, ReturnOpportunitiesDTO>()
    .IncludeMembers(source => source.opportunity, source => source.User);
config.CreateMap<Opportunity, ReturnOpportunitiesDTO>();
config.CreateMap<AppUser, ReturnOpportunitiesDTO>()
    .ForMember(
        dest => dest.UserFullName,
        options => options.MapFrom(source =>
            string.Join(
                " ",
                source.FirstName,
                source.MiddleName,
                source.LastName)));

用法:

var mappedDtos = mapper.Map<List<ReturnOpportunitiesDTO>>(assignedOpportuniesFromDatabase);