asp.net AutoMapperMap配置文件配置

xxhby3vn  于 2023-06-25  发布在  .NET
关注(0)|答案(1)|浏览(137)

我正在尝试获取包含用户的“组”。然而,我遇到了各种与自动Map器相关的问题。
DTO包含在命令中
组有许多用户,但用户只有一个组。
我想尝试让包括用户在内的用户组使用它。
获取组异步

public class GetGroupsAsync
{
    public class GroupsAsyncQuery : IRequest<IEnumerable<GroupsAsyncQueryResult>> {}

    public class GroupsAsyncQueryResult
    {
        public Guid Id { get; set;}
        public string GroupCode { get; set;}
        public string GroupName { get; set;}
        public IReadOnlyList<GetUsersAsync.UsersAsyncQueryResult> Users { get; set;}
    }
    public class Handler : IRequestHandler<GroupsAsyncQuery, IEnumerable<GroupsAsyncQueryResult>>
    {
        private readonly IMapper _mapper;
        private readonly DataContext _dataContext;

        public Handler(IMapper mapper, DataContext dataContext)
        {
            _mapper = mapper;
            _dataContext = dataContext;
        }

        public async Task<IEnumerable<GroupsAsyncQueryResult>> Handle(GroupsAsyncQuery request,
            CancellationToken cancellationToken)
        {
            var groups = await _dataContext.Groups
                .Include(x => x.UsersCollection)
                .ToListAsync(cancellationToken);
            if (groups == null)
            {
                throw new NoGroupsFoundExceptions();
            }

            var result = _mapper.Map<IEnumerable<GroupsAsyncQueryResult>>(groups);
            return result;
        }
    }
   
}

集团

public class Groups
{
    [Column("GroupId")]
    public Guid Id {get;set;}
    public string GroupCode {get;set;}
    public string GroupName {get;set;}
    [DataType(DataType.Date)]
    public DateTime DateAdded {get;set;} 
    public ICollection<Users> UsersCollection {get;set;}
}

用户

public class Users
{
    [Column("UserId")]
    public Guid Id {get;set;}
    public string UserCode { get; set; }
    public string FullName { get; set; }
    public string UserName { get; set;}
    public string Password { get; set; }
    public Guid GroupId { get; set; }
    public Groups Group { get; set; }
}

Map配置文件

public MapperProfiles()
    {
        CreateMap<Groups, GetGroupsAsync.GroupsAsyncQueryResult>()
            .ForMember(dest => dest.Id, opt => opt.MapFrom(src => src.UsersCollection.Select(x => x.FullName)));
    }

错误

"Error mapping types.\r\n\r\nMapping types:\r\nList`1 -> IEnumerable`1\r\nSystem.Collections.Generic.List`1[[MineralWaterMonitoring.Domain.Groups, MineralWaterMonitoring, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] -> System.Collections.Generic.IEnumerable`1[[MineralWaterMonitoring.Features.Group.GetGroupsAsync+GroupsAsyncQueryResult, MineralWaterMonitoring, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]"

我们非常感谢任何帮助。谢谢
我尝试更改配置并使用IReadOnlyList。

toiithl6

toiithl61#

您误解了ForMember的用法,请检查this link以了解如何正确使用它。
请尝试修改您的代码:

public MapperProfiles()
{
    CreateMap<Groups, GetGroupsAsync.GroupsAsyncQueryResult>()
        .ForMember(dest => dest.Users, opt => opt.MapFrom(src => src.UsersCollection)); 
    CreateMap<Users, UsersAsyncQueryResult>();
}

测试结果:

相关问题