C
C#2y ago
_vegabyte_

❔ AutoMapper Mapping Profiles Configuration

Hello, I'm trying to obtain the "Groups" with the users included. I'm however experiencing various Auto Mapper-related issues. 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;
}
}

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

}
Any help we much appreciated. Thanks
28 Replies
_vegabyte_
_vegabyte_OP2y ago
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;
}
}
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;
}
}
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)));
}
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]]"
"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]]"
M B V R K
M B V R K2y ago
I want to provide a help but i just get confused, why did you named that class with Groups I mean why you don't name it to Group ?
_vegabyte_
_vegabyte_OP2y ago
Sorry for the confusion. My IDE just recommend it maybe I'll change it later. Your help is much appreciated
M B V R K
M B V R K2y ago
Well I think I get the issue you need to change this
CreateMap<Groups, GetGroupsAsync.GroupsAsyncQueryResult>()
CreateMap<Groups, GetGroupsAsync.GroupsAsyncQueryResult>()
to
CreateMap<Groups, IEnumerable<GetGroupsAsync.GroupsAsyncQueryResult>>()
CreateMap<Groups, IEnumerable<GetGroupsAsync.GroupsAsyncQueryResult>>()
_vegabyte_
_vegabyte_OP2y ago
Unfortunate still getting the issue
"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]]"
"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]]"
M B V R K
M B V R K2y ago
Did you rebuilt your project, try it and let me know if it get fixed
_vegabyte_
_vegabyte_OP2y ago
yes I'll already rebuilt the project but still getting the error
M B V R K
M B V R K2y ago
Is Domain a spearated project ??
_vegabyte_
_vegabyte_OP2y ago
No its on the same project Domain is a Folder containing my Entities
_vegabyte_
_vegabyte_OP2y ago
GitHub
GitHub - aldrin0408av/MineralWaterMonitoringSystem
Contribute to aldrin0408av/MineralWaterMonitoringSystem development by creating an account on GitHub.
M B V R K
M B V R K2y ago
Please give me a bit of time, I'm trying to investigate about the issue in your code
_vegabyte_
_vegabyte_OP2y ago
Sure Sir, Take your time. Thank you!
M B V R K
M B V R K2y ago
First of you need to change the
CreateMap<Groups, GetGroupsAsync.GroupsAsyncQuery>()
CreateMap<Groups, GetGroupsAsync.GroupsAsyncQuery>()
to
CreateMap<Groups, GetGroupsAsync.GroupsAsyncQueryResult>()
CreateMap<Groups, GetGroupsAsync.GroupsAsyncQueryResult>()
But you will still get a runtime error : because of the Id in GroupsAsyncQueryResult is Guidand the FullName in Users is string Can you just tell me what you want to do in the MappingProfile ??
_vegabyte_
_vegabyte_OP2y ago
I want the result would be like this { "success": true, "data": [ { "id": "780c5114-4e55-4878-0218-08db6648b3a6", "groupCode": "3FCOR", "groupName": "3rd Floor Corporate", "users": [] } ], "messages": [] } I want to get the groups including the user in that group
M B V R K
M B V R K2y ago
Alright the GroupsAsyncQueryResult 's Users should be lke this :
public ICollection<Domain.Users> Users
{
get;
set;
}
public ICollection<Domain.Users> Users
{
get;
set;
}
Then the mapping should be :
CreateMap<Groups, GetGroupsAsync.GroupsAsyncQueryResult>()
.ForMember(dest => dest.Users, opt => opt.MapFrom(src => src.UsersCollection));
CreateMap<Groups, GetGroupsAsync.GroupsAsyncQueryResult>()
.ForMember(dest => dest.Users, opt => opt.MapFrom(src => src.UsersCollection));
M B V R K
M B V R K2y ago
This is the version of your project that I modified
M B V R K
M B V R K2y ago
Let me know if you the issue get fixed and the goal get achieved
_vegabyte_
_vegabyte_OP2y ago
The error is fixed and this is the output.
{
"success": true,
"data": [
{
"id": "780c5114-4e55-4878-0218-08db6648b3a6",
"groupCode": "3FCOR",
"groupName": "3rd Floor Corporate",
"users": [
{
"id": "a59c1fbe-cf73-4052-c442-08db6648ebd6",
"userCode": null,
"fullName": "Aldrin Vega",
"userName": "Aldrin",
"password": "Vega",
"groupId": "780c5114-4e55-4878-0218-08db6648b3a6",
"group": {
"id": "780c5114-4e55-4878-0218-08db6648b3a6",
"groupCode": "3FCOR",
"groupName": "3rd Floor Corporate",
"dateAdded": "0001-01-01T00:00:00",
"usersCollection": [
null,
{
"id": "83bfc054-1021-4204-d9e8-08db6649b9bb",
"userCode": null,
"fullName": "Aldrin Vega",
"userName": "Minari",
"password": "Aldrin004",
"groupId": "780c5114-4e55-4878-0218-08db6648b3a6",
"group": null
}
]
}
},
{
"id": "83bfc054-1021-4204-d9e8-08db6649b9bb",
"userCode": null,
"fullName": "Aldrin Vega",
"userName": "Minari",
"password": "Aldrin004",
"groupId": "780c5114-4e55-4878-0218-08db6648b3a6",
"group": {
"id": "780c5114-4e55-4878-0218-08db6648b3a6",
"groupCode": "3FCOR",
"groupName": "3rd Floor Corporate",
"dateAdded": "0001-01-01T00:00:00",
"usersCollection": [
{
"id": "a59c1fbe-cf73-4052-c442-08db6648ebd6",
"userCode": null,
"fullName": "Aldrin Vega",
"userName": "Aldrin",
"password": "Vega",
"groupId": "780c5114-4e55-4878-0218-08db6648b3a6",
"group": null
},
null
]
}
}
]
}
],
}
{
"success": true,
"data": [
{
"id": "780c5114-4e55-4878-0218-08db6648b3a6",
"groupCode": "3FCOR",
"groupName": "3rd Floor Corporate",
"users": [
{
"id": "a59c1fbe-cf73-4052-c442-08db6648ebd6",
"userCode": null,
"fullName": "Aldrin Vega",
"userName": "Aldrin",
"password": "Vega",
"groupId": "780c5114-4e55-4878-0218-08db6648b3a6",
"group": {
"id": "780c5114-4e55-4878-0218-08db6648b3a6",
"groupCode": "3FCOR",
"groupName": "3rd Floor Corporate",
"dateAdded": "0001-01-01T00:00:00",
"usersCollection": [
null,
{
"id": "83bfc054-1021-4204-d9e8-08db6649b9bb",
"userCode": null,
"fullName": "Aldrin Vega",
"userName": "Minari",
"password": "Aldrin004",
"groupId": "780c5114-4e55-4878-0218-08db6648b3a6",
"group": null
}
]
}
},
{
"id": "83bfc054-1021-4204-d9e8-08db6649b9bb",
"userCode": null,
"fullName": "Aldrin Vega",
"userName": "Minari",
"password": "Aldrin004",
"groupId": "780c5114-4e55-4878-0218-08db6648b3a6",
"group": {
"id": "780c5114-4e55-4878-0218-08db6648b3a6",
"groupCode": "3FCOR",
"groupName": "3rd Floor Corporate",
"dateAdded": "0001-01-01T00:00:00",
"usersCollection": [
{
"id": "a59c1fbe-cf73-4052-c442-08db6648ebd6",
"userCode": null,
"fullName": "Aldrin Vega",
"userName": "Aldrin",
"password": "Vega",
"groupId": "780c5114-4e55-4878-0218-08db6648b3a6",
"group": null
},
null
]
}
}
]
}
],
}
I want to get the user will be the Name only is that possible?
M B V R K
M B V R K2y ago
So you want to get all Groups and every group should include the Name of the user ???
_vegabyte_
_vegabyte_OP2y ago
Yes Sir,
M B V R K
M B V R K2y ago
hmmm, give me a bit of time I will change somethings in your project
_vegabyte_
_vegabyte_OP2y ago
Okay sir, Thanks
M B V R K
M B V R K2y ago
I did a bit change in your code, you can take a look at it here an tell me if this good for design you follow? and is it works ?
_vegabyte_
_vegabyte_OP2y ago
Sir i get some error
{
"success": false,
"data": null,
"messages": [
"Exception has been thrown by the target of an invocation."
]
}
{
"success": false,
"data": null,
"messages": [
"Exception has been thrown by the target of an invocation."
]
}
M B V R K
M B V R K2y ago
Can you start the project in debug mode and do a screenshot to the Exception ?
_vegabyte_
_vegabyte_OP2y ago
_vegabyte_
_vegabyte_OP2y ago
Expression 'dest => dest.User.FullName' must resolve to top-level member and not any child object's properties. You can use ForPath, a custom resolver on the child type or the AfterMap option instead. (Parameter 'lambdaExpression')
Expression 'dest => dest.User.FullName' must resolve to top-level member and not any child object's properties. You can use ForPath, a custom resolver on the child type or the AfterMap option instead. (Parameter 'lambdaExpression')
Hi, just tweaking the code and it works as expected Thank You for your help
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server