【问题标题】:How Map Multiple related Entities to one DTO Object using AutoMapper EF Core如何使用 AutoMapper EF Core 将多个相关实体映射到一个 DTO 对象
【发布时间】:2020-07-04 03:51:23
【问题描述】:

我的 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; }
}

AppUser 类

 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>();
    }

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

【问题讨论】:

    标签: c# asp.net-core automapper


    【解决方案1】:

    您的场景只是扁平化复杂对象的另一个示例。您在子对象中有属性,您希望将其带到底层,同时仍利用 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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多