Home > Mobile >  Auto mapper makes error when mapping class with another class properties
Auto mapper makes error when mapping class with another class properties

Time:11-15

I have following two class structures, its CASADetialsResponse as follows,

public class CASADetialsResponse
{
    public string status { get; set; }
    public string message { get; set; }
    public List<object> validation { get; set; }
    public Data data { get; set; }
}

public class LinkedCard
{
    public string cardNumber { get; set; }
}

public class JointParty
{
    public string jointName { get; set; }
}

public class Account
{
    public string accountNumber { get; set; }
    public string accountNickName { get; set; }
    public List<LinkedCard> linkedCards { get; set; }
    public List<JointParty> jointParty { get; set; }
    public List<object> productValidation { get; set; }
}

public class Transaction
{
    public string accountNumber { get; set; }
    public string valueDate { get; set; }
    public string transactionDetails { get; set; }
    public string postDate { get; set; }

}

public class Data
{
    public Account account { get; set; }
    public List<Transaction> transactions { get; set; }
}

And the CASADetails class structure as follows,

public class CASADetails
{
    public string status { get; set; }
    public string message { get; set; }
    public List<object> validation { get; set; }
    public Data data { get; set; }
}

public class LinkedCard
{
    public string cardNumber { get; set; }
}

public class JointParty
{
    public string jointName { get; set; }
}

public class Account
{
    public string accountNumber { get; set; }
    public string accountNickName { get; set; }
    public List<LinkedCard> linkedCards { get; set; }
    public List<JointParty> jointParty { get; set; }
    public List<object> productValidation { get; set; }
}

public class Transaction
{
    public string accountNumber { get; set; }
    public string valueDate { get; set; }
    public string transactionDetails { get; set; }
    public string postDate { get; set; }

}

public class Data
{
    public Account account { get; set; }
    public List<Transaction> transactions { get; set; }
}

When I use _mapper.Map<CASADetialsResponse>(casaResponse); I'm getting this error:

Error mapping types:

Mapping types: CASADetails -> CASADetialsResponse AccountManagement.Domain.CASADetails -> AccountManagement.Application.Dtos.CASADetails.CASADetialsResponse Type Map configuration: CASADetails -> CASADetialsResponse AccountManagement.Domain.CASADetails -> AccountManagement.Application.Dtos.CASADetails.CASADetialsResponse Destination Member: data

This is how I map the two classes,

public class CASADetailsProfile : Profile
{
    public CASADetailsProfile()
    {
        // Source -> Target
        CreateMap<CASADetialsRequest, CASADetails>();
        CreateMap<CASADetails, CASADetialsResponse>();
    }
}

I just commented the public Data data { get; set; } from the both classes, and without any error its worked. I think problem is with that line. may I know the reason? please help me

CodePudding user response:

The namespace for Data class is different, even though their content is the same.

Delete these

enter image description here

and refer to source Data class namespace or

If you don't want to delete them, add the following configuration

public class CASADetailsProfile: Profile
{
    public CASADetailsProfile()
    {
        // Source -> Target
        CreateMap<CASADetialsRequest, CASADetails>();
        CreateMap<CASADetails, CASADetialsResponse>();

        
        CreateMap<Sources.Data, Destinations.Data>();
        CreateMap<Sources.Account, Destinations.Account>();
        CreateMap<Sources.Transaction, Destinations.Transaction>();
        CreateMap<Sources.Transaction, Destinations.Account>();
        CreateMap<Sources.LinkedCard, Destinations.LinkedCard>();
        CreateMap<Sources.JointParty, Destinations.JointParty>();
    }
}

Change Sources namespace and Destinations namespace to the namespace that you have

  • Related