Ends in:
2 DAYS
14 HRS
32 MIN
44 SEC
Ends in:
2 D
14 H
32 M
44 S
Open Closed

Automapper config for nested dtos #1855


User avatar
0
Neozzz created

I have the following classes:

public class Employee : FullAuditedEntity<Guid>
{
    <<<<properties....>>>
    public ICollection<Contact> Contacts {get;set;}
}

public class Contact : FullAuditedEntity<Guid>
{
    <<<properties...>>>
    public ICollection<Address> Addresses {get;set;}
}

public class Address : FullAuditedEntity<Guid>
{
     <<<properties...>>>   
}

Which means an employee can have multiple contacts and each contact can have multiple addresses.

Now I have the following Dtos to insert 1 employee record.

public class EmployeeDto : FullAuditedEntityDto<Guid>
{
    <<<<properties....>>>
    public ICollection<ContactDto> Contacts {get;set;}
}

public class ContactDto : FullAuditedEntityDto<Guid>
{
    <<<properties...>>>
    public ICollection<AddressDto> Addresses {get;set;}
}

public class AddressDto : FullAuditedEntityDto<Guid>
{
     <<<properties...>>>   
}

I have the following automapper configuration for layer 1 mapping, where we map employee to employeeDto and contact to contactdto.

CreateMap<Employee, EmployeeDto>()
                .ForMember(dest => dest.Contacts,
                            opt => opt.MapFrom(src => src.Contacts));

How can we map the address in contact to the addressdto in contactdto?

Or is there a more reliable method. Thank you


7 Answer(s)
  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    I think you can create three maps.

    CreateMap<Employee, EmployeeDto>()
    CreateMap<Contact, ContactDto>()
    CreateMap<AddressDto, AddressDto>()
    

    https://docs.automapper.org/en/latest/Getting-started.html

  • User Avatar
    0
    Neozzz created

    there are 3 maps apart from this. but i am getting error saying there is mapping issue inside employee with the contact entity.

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    Please share the error details.

  • User Avatar
    0
    Neozzz created
    2021-09-15 10:34:23.663 +04:00 [ERR]
    ---------- RemoteServiceErrorInfo ----------
    {
      "code": null,
      "message": "An internal error occurred during your request!",
      "details": null,
      "data": {},
      "validationErrors": null
    }
    
    2021-09-15 10:34:23.665 +04:00 [ERR] Error mapping types.
    
    Mapping types:
    Employee -> EmployeeDto
    ZW.EmployeeMgmtSvc.Employees.Employee -> ZW.EmployeeMgmtSvc.Employees.EmployeeDto
    
    Type Map configuration:
    Employee -> EmployeeDto
    ZW.EmployeeMgmtSvc.Employees.Employee -> ZW.EmployeeMgmtSvc.Employees.EmployeeDto
    
    Destination Member:
    Contacts
    
    AutoMapper.AutoMapperMappingException: Error mapping types.
    
    Mapping types:
    Employee -> EmployeeDto
    ZW.EmployeeMgmtSvc.Employees.Employee -> ZW.EmployeeMgmtSvc.Employees.EmployeeDto
    
    Type Map configuration:
    Employee -> EmployeeDto
    ZW.EmployeeMgmtSvc.Employees.Employee -> ZW.EmployeeMgmtSvc.Employees.EmployeeDto
    
    Destination Member:
    Contacts
    
     ---> AutoMapper.AutoMapperMappingException: Missing type map configuration or unsupported mapping.
    
    Mapping types:
    Contact -> ContactDto
    ZW.EmployeeMgmtSvc.Contacts.Contact -> ZW.EmployeeMgmtSvc.Contacts.ContactDto
       at lambda_method3391(Closure , Contact , ContactDto , ResolutionContext )
       at lambda_method3390(Closure , Object , EmployeeDto , ResolutionContext )
       --- End of inner exception stack trace ---
       at lambda_method3390(Closure , Object , EmployeeDto , ResolutionContext )
       at Volo.Abp.ObjectMapping.DefaultObjectMapper.Map[TSource,TDestination](TSource source)
       at ZW.EmployeeMgmtSvc.Employees.EmployeeAppService.CreateAsync(EmployeeCreateAndUpdateDto input) in D:\Workspace\EmployeeMgmtSvc\aspnet-core\src\ZW.EmployeeMgmtSvc.Application\Employees\EmployeeAppService.cs:line 90
       at Castle.DynamicProxy.AsyncInterceptorBase.ProceedAsynchronous[TResult](IInvocation invocation, IInvocationProceedInfo proceedInfo)
       at Volo.Abp.Castle.DynamicProxy.CastleAbpMethodInvocationAdapterWithReturnValue`1.ProceedAsync()
       at Volo.Abp.GlobalFeatures.GlobalFeatureInterceptor.InterceptAsync(IAbpMethodInvocation invocation)
       at Volo.Abp.Castle.DynamicProxy.CastleAsyncAbpInterceptorAdapter`1.InterceptAsync[TResult](IInvocation invocation, IInvocationProceedInfo proceedInfo, Func`3 proceed)
       at Castle.DynamicProxy.AsyncInterceptorBase.ProceedAsynchronous[TResult](IInvocation invocation, IInvocationProceedInfo proceedInfo)
       at Volo.Abp.Castle.DynamicProxy.CastleAbpMethodInvocationAdapterWithReturnValue`1.ProceedAsync()
       at Volo.Abp.Auditing.AuditingInterceptor.InterceptAsync(IAbpMethodInvocation invocation)
       at Volo.Abp.Castle.DynamicProxy.CastleAsyncAbpInterceptorAdapter`1.InterceptAsync[TResult](IInvocation invocation, IInvocationProceedInfo proceedInfo, Func`3 proceed)
       at Castle.DynamicProxy.AsyncInterceptorBase.ProceedAsynchronous[TResult](IInvocation invocation, IInvocationProceedInfo proceedInfo)
       at Volo.Abp.Castle.DynamicProxy.CastleAbpMethodInvocationAdapterWithReturnValue`1.ProceedAsync()
       at Volo.Abp.Validation.ValidationInterceptor.InterceptAsync(IAbpMethodInvocation invocation)
       at Volo.Abp.Castle.DynamicProxy.CastleAsyncAbpInterceptorAdapter`1.InterceptAsync[TResult](IInvocation invocation, IInvocationProceedInfo proceedInfo, Func`3 proceed)
       at Castle.DynamicProxy.AsyncInterceptorBase.ProceedAsynchronous[TResult](IInvocation invocation, IInvocationProceedInfo proceedInfo)
       at Volo.Abp.Castle.DynamicProxy.CastleAbpMethodInvocationAdapterWithReturnValue`1.ProceedAsync()
       at Volo.Abp.Uow.UnitOfWorkInterceptor.InterceptAsync(IAbpMethodInvocation invocation)
       at Volo.Abp.Castle.DynamicProxy.CastleAsyncAbpInterceptorAdapter`1.InterceptAsync[TResult](IInvocation invocation, IInvocationProceedInfo proceedInfo, Func`3 proceed)
       at lambda_method2489(Closure , Object )
       at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.AwaitableObjectResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
       at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeActionMethodAsync>g__Awaited|12_0(ControllerActionInvoker invoker, ValueTask`1 actionResultValueTask)
       at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.&lt;InvokeNextActionFilterAsync&gt;g__Awaited|10_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
       at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context)
       at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
       at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.&lt;InvokeInnerFilterAsync&gt;g__Awaited|13_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
       at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.&lt;InvokeNextExceptionFilterAsync&gt;g__Awaited|25_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
    2021-09-15 10:34:23.677 +04:00 [INF] Executing ObjectResult, writing value of type 'Volo.Abp.Http.RemoteServiceErrorResponse'.
    2021-09-15 10:34:23.690 +04:00 [INF] Executed action ZW.EmployeeMgmtSvc.Employees.EmployeeAppService.CreateAsync (ZW.EmployeeMgmtSvc.Application) in 7668.1201ms
    2021-09-15 10:34:23.690 +04:00 [INF] Executed endpoint 'ZW.EmployeeMgmtSvc.Employees.EmployeeAppService.CreateAsync (ZW.EmployeeMgmtSvc.Application)'
    2021-09-15 10:34:24.889 +04:00 [DBG] Added 0 entity changes to the current audit log
    2021-09-15 10:34:24.889 +04:00 [DBG] Added 0 entity changes to the current audit log
    
  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    Can you share a simple project?

  • User Avatar
    0
    Neozzz created

    Hi @maliming,

    The code I am using has code pertaining to our active project development. If possible could you please remote desktop and see the code?

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    https://zoom.us/j/97169875896?pwd=b2ZFRjc3VFZUTkZpeFE2TlN4NjZwdz09

Made with ❤️ on ABP v9.1.0-preview. Updated on December 02, 2024, 12:35