Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AutoMapper Not Mapping... No Errors Thrown

Tags:

c#

automapper

Here's that I have in my WPF application:

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() is called once on application start.

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

Model:

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

Extension method:

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

I call a servicestack service which returns a List<SO.Services.Data.ServiceModel.Types.CustomerSearchResult> ... when I use the ToModels extension method against it, it returns a List with 0 records, even though the source list had 25k or so records.

I'm stumped.

like image 608
Chris Klepeis Avatar asked Oct 16 '25 16:10

Chris Klepeis


1 Answers

In your CreateMaps() you would specify the object mappings, not the list mapping.

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

And then in your ToModels() you do

Mapper.Map<List<CustomerSearchResult>, List<SO.Services.Data.ServiceModel.Types.CustomerSearchResult>>(customerSearchList);
like image 69
tc44 Avatar answered Oct 18 '25 09:10

tc44



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!