使用另一个 class 属性映射 class 时,自动映射器出错

Auto mapper makes error when mapping class with another class properties

我有以下两个class结构,其CASADetialsResponse如下,

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

CASADetailsclass结构如下,

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

当我使用 _mapper.Map<CASADetialsResponse>(casaResponse); 时出现此错误:

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

这就是我映射两个 classes,

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

我刚刚评论了两个 class 中的 public Data data { get; set; },并且没有任何错误,它起作用了。我认为问题在于那条线。我可以知道原因吗?请帮助我

数据 class 的命名空间不同,尽管它们的内容相同。

删除这些

并引用源数据 class 命名空间或

如果不想删除,添加如下配置

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.LinkedCard, Destinations.LinkedCard>();
        CreateMap<Sources.JointParty, Destinations.JointParty>();
    }
}

将 Sources 命名空间和 Destinations 命名空间更改为您拥有的命名空间