AutoMapper 不包括响应中的 GetUserDto

AutoMapper is not including the GetUserDto in response

AutoMapper Class:

namespace ef_practice
{
    public class AutoMapperProfile : Profile
    {
        public AutoMapperProfile()
        {
            CreateMap<Character, GetCharacterDto>();
            CreateMap<AddCharacterDto, Character>();
            CreateMap<Weapon, GetWeaponDto>();
            CreateMap<User, GetUserDto>();
        }
    }
}

服务层:

public async Task<ServiceResponse<GetCharacterDto>> GetSingle(int id)
        {
            var serviceResponse = new ServiceResponse<GetCharacterDto>();
            var dbCharacter = await _context.Characters
            .Include(c => c.User)
            .FirstOrDefaultAsync(ele => ele.Id == id && ele.User.Id == 1);
            serviceResponse.Data = _mapper.Map<GetCharacterDto>(dbCharacter);
            return serviceResponse;
        }

GetCharacterDto Class:

public class GetCharacterDto
    {
        public int Id { get; set; }
        public string Name { get; set; } = "Frodo";
        public int HitPoints { get; set; } = 100;
        public int Strength { get; set; } = 10;
        public int Defense { get; set; } = 10;
        public int Intelligence { get; set; } = 10;
        public RpgClass Class { get; set; } = RpgClass.Knight;
        public GetUserDto User = new GetUserDto();
    }

GetUserDto Class:

namespace ef_practice.Dtos.User
{
    public class GetUserDto
    {
        public int Id { get; set; }
        public string Username { get; set; } = "";
    }
}

回复:

{
  "data": {
    "id": 2,
    "name": "string",
    "hitPoints": 0,
    "strength": 10,
    "defense": 0,
    "intelligence": 0,
    "class": 0
  },
  "success": true,
  "message": ""
}

响应数据属于模型 GetCharacterDto,其中包含 GetUserDto,但在响应中不包含用户对象。我不确定这里出了什么问题。谁能帮我解决一下。

GetCharacterDto class 中,User 是一个字段而不是 属性。

通过添加 getter 和 setter 将其更改为 属性。

public class GetCharacterDto
{
    ...

    public GetUserDto User { get; set; } = new GetUserDto();
}