gpt4 book ai didi

c# - AutoMapper 未映射...未抛出错误

转载 作者:行者123 更新时间:2023-11-30 14:27:16 25 4
gpt4 key购买 nike

这是我的 WPF 应用程序中的内容:

public static class MappingCreator
{
public static void CreateMaps()
{
Mapper.CreateMap<SO.Services.Data.ServiceModel.Types.Customer, Customer>();
Mapper.CreateMap<List<SO.Services.Data.ServiceModel.Types.CustomerSearchResult>, List<CustomerSearchResult>>();

Mapper.AssertConfigurationIsValid();
}
}

CreateMaps()在应用程序启动时调用一次。

DTO:

namespace SO.Services.Data.ServiceModel.Types
{
[DataContract]
public class CustomerSearchResult
{
[DataMember]
public int CustomerId { get; set; }
[DataMember]
public string AccountType { get; set; }
[DataMember]
public string ShortName { get; set; }
[DataMember]
public string LegacyName { get; set; }
[DataMember]
public string LegacyContactName { get; set; }
[DataMember]
public string City { get; set; }
[DataMember]
public string StateAbbreviation { get; set; }
[DataMember]
public string Country { get; set; }
[DataMember]
public string PostalCode { get; set; }
}
}

型号:

namespace SO.Models
{
public class CustomerSearchResult : BindableBase
{
public int CustomerId { get; set; }
public string AccountType { get; set; }
public string ShortName { get; set; }
public string LegacyName { get; set; }
public string LegacyContactName { get; set; }
public string City { get; set; }
public string StateAbbreviation { get; set; }
public string Country { get; set; }
public string PostalCode { get; set; }
}
}

扩展方法:

public static class DtoMappingExtensions
{
public static List<CustomerSearchResult> ToModels(this List<SO.Services.Data.ServiceModel.Types.CustomerSearchResult> customerSearchList)
{
return Mapper.Map<List<SO.Services.Data.ServiceModel.Types.CustomerSearchResult>, List<CustomerSearchResult>>(customerSearchList);
}
}

我调用一个返回 List<SO.Services.Data.ServiceModel.Types.CustomerSearchResult> 的 servicestack 服务...当我对它使用 ToModels 扩展方法时,它返回一个包含 0 条记录的列表,即使源列表有 25k 左右的记录。

我很难过。

最佳答案

在您的 CreateMaps() 中,您将指定对象映射,而不是列表映射。

Mapper.CreateMap<SO.Services.Data.ServiceModel.Types.CustomerSearchResult, CustomerSearchResult>().ReverseMap();

然后在你的 ToModels() 中做

Mapper.Map<List<CustomerSearchResult>, List<SO.Services.Data.ServiceModel.Types.CustomerSearchResult>>(customerSearchList);

关于c# - AutoMapper 未映射...未抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33308583/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com