I'm trying to obtain the "Groups" with the users included. I'm however experiencing various Auto Mapper-related issues.
The DTO is included in the Command
The Groups has many Users but Users has only one group.
I want to try to get groups including the Users using it.
GetGroupsAsync
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;
}
}
}
Groups
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;}
}
Users
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; }
}
Mapping Profiles
public MapperProfiles()
{
CreateMap<Groups, GetGroupsAsync.GroupsAsyncQueryResult>()
.ForMember(dest => dest.Id, opt => opt.MapFrom(src => src.UsersCollection.Select(x => x.FullName)));
}
Error
"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]]"
Any help we much appreciated. Thanks
I tried changing the configuration and use IReadOnlyList.