Blazor:如何使用 autoMapper 将两个实体映射到一个 DTO?

问题描述 投票:0回答:1

资料实体:

public class Profile
    {
        public int PId { get; set; }
        public int PCode { get; set; }
        [ForeignKey("Person")]
        public int PersonId { get; set; }
        public DateTimeOffset? TashkilDate { get; set; }
        public int? Category { get; set; }
        public bool IsEstelam { get; set; }
        public bool IsTaeed { get; set; }
        //navigation
        //MtoM
        public Person person { get; set; }
    }

个人实体:

public class Person
    {
        public int PersonId { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string FatherName { get; set; }
        [ForeignKey("Jens")]
        public int? JensId { get; set; }
        public string? NationalCode { get; set; }
        public string? ShSh { get; set; }
        public DateTimeOffset? BirthDate { get; set; }
        public string? OldFamily { get; set; }
        public int? PassportSerial { get; set; }

        //navigations
        //MtoM
        public ICollection<Profile> profiles { get; set; }

    }

ProfilePersonDTO:

public class ProfilePersonDTO
    {
        public int PId { get; set; }
        public int PCode { get; set; }
        public DateTimeOffset? TashkilDate { get; set; }
        public bool IsEstelam { get; set; }
        public bool IsTaeed { get; set; }
        
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string FatherName { get; set; }
        public string? NationalCode { get; set; }
        public string? ShSh { get; set; }

        public Person person { get; set; }
    }

我在服务中的查询:

var query = _dbContext.Profile.Where(u => u.PersonId == PersonIdParam);
var resultQuery = query
                       .Include(u => u.person)
                       .ProjectTo<ProfilePersonDTO>(_mapperConfiguration)
                       .AsAsyncEnumerable();

我写这个是为了映射:

CreateMap<Profile, ProfilePersonDTO>().ReverseMap();

但是 Person 字段没有映射到 DTO 字段。这意味着“FirstName、LastName、FatherName、NationalCode 和 ShSh”为空。

我应该如何使用 autoMapper 将这两个实体映射到 DTO? (填写所有字段)

c# .net blazor automapper dto
1个回答
0
投票

我问另一个地方&一个人回答我! 我应该像这样将“ForMap”添加到“CreateMap”: ... 创建地图() .ForMember(dst => dst.FirstName, opt => opt.MapFrom(src => src.person.FirstName)) .ForMember(dst => dst.LastName, opt => opt.MapFrom(src => src.person.LastName)) .ForMember(dst => dst.FatherName, opt => opt.MapFrom(src => src.person.FatherName)); ...

© www.soinside.com 2019 - 2024. All rights reserved.