C
C#14mo 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_14mo 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 K14mo 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_14mo 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 K14mo 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_14mo 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 K14mo ago
Did you rebuilt your project, try it and let me know if it get fixed
_vegabyte_
_vegabyte_14mo ago
yes I'll already rebuilt the project but still getting the error
M B V R K
M B V R K14mo ago
Is Domain a spearated project ??
_vegabyte_
_vegabyte_14mo ago
No its on the same project Domain is a Folder containing my Entities
_vegabyte_
_vegabyte_14mo 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 K14mo ago
Please give me a bit of time, I'm trying to investigate about the issue in your code
_vegabyte_
_vegabyte_14mo ago
Sure Sir, Take your time. Thank you!
M B V R K
M B V R K14mo 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_14mo 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 K14mo 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 K14mo ago
This is the version of your project that I modified
M B V R K
M B V R K14mo ago
Let me know if you the issue get fixed and the goal get achieved
_vegabyte_
_vegabyte_14mo 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 K14mo ago
So you want to get all Groups and every group should include the Name of the user ???
_vegabyte_
_vegabyte_14mo ago
Yes Sir,
M B V R K
M B V R K14mo ago
hmmm, give me a bit of time I will change somethings in your project
_vegabyte_
_vegabyte_14mo ago
Okay sir, Thanks
M B V R K
M B V R K14mo 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_14mo 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 K14mo ago
Can you start the project in debug mode and do a screenshot to the Exception ?
_vegabyte_
_vegabyte_14mo ago
_vegabyte_
_vegabyte_14mo 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
Accord14mo 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
More Posts
❔ CIM Class MDM_RemoteWipe Method QuestionHi friends, I am attempting to call a method using `CimSession.InvokeMethod()` called `doWipeProtec❔ how to reference all dlls in separate folder such as "bin" or "lib"self explanatory title✅ ✅ Vararg P/Invoke (x86) throws BadImageFormatException (0x80131124 "Index not found")I am attempting to perform vararg P/Invokes following signatures I see on pinvoke.net and, e.g., htt✅ Attachment upload to Jira through a custom application using AJAX and ASP .NET Core 6 - Rest SharpSo I have this ajax snippet with which I send the files to my custom controller, using the IFormFile❔ I looking for Game Engine / Framework!Is there any cool Game Engine / Framework✅ Weird values on accessing Database using EF DbContext under throughput load testHello! I'm testing an ASP.NET application that uses an SQL Database. To access this database I defiProperties of IConfigurationSection.Get are null?I have a very simple test case set up using Microsoft.Extensions.Configuration and Microsoft.ExtensiHow do I pass data from my partial view to my main layout in ASP Core?I have this code in my partial view: ``` // Partial View Lesson.html @{ ViewData["LessonTitle"] ✅ [wpf] how to solve autometically closed new window when I create new window..In StickyNotesView.xaml , I create new Window but If I click '+' that new window not only undisplayePlugin attempts to load dependency again, despite it already being loaded?I have a .NET (.NET 7, for posterity) hosting from C++ situation. I followed the .NET hosting tutori