Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why won't this AutoMapper configuration map properly?

Tags:

c#

automapper

Given the following code below, why is it that I keep getting an exception during the mapping phase? Are the 2 DTOs really that different? Here's the line of code from the symbol server pdb that is throwing the exception.

throw new AutoMapperMappingException(context, "Missing type map configuration or unsupported mapping.");

What's really killing me is that @jbogard has done a killer job of exception handling and instrumentation with regards to AutoMapper, there is plenty of contextual data for both the source and destination objects, and the state of the mapper when an exception is thrown ..and I still can't figure it out.

void Main()
{
    var config = new MapperConfiguration(cfg =>
    {
        cfg.ShouldMapProperty = p => p.GetMethod.IsPublic || p.GetMethod.IsVirtual;
        cfg.CreateMap<Customer, Customer2>()
        .ReverseMap();
    });

    config.AssertConfigurationIsValid();

    Customer request = new Customer
    {
        FirstName = "Hello", LastName = "World"
    };
    request.FullName = request.FirstName + " " + request.LastName;

    Customer2 entity = Mapper.Map<Customer, Customer2>(request);


    Console.WriteLine("finished");
}



public class Customer
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string FullName { get; set; }
}

[Serializable()]
public partial class Customer2
{
    private string _firstName;
    private string _lastName;
    private string _fullName;

    public virtual string FirstName
    {
        get
        {
            return this._firstName;
        }
        set
        {
            this._firstName = value;
        }
    }
    public virtual string LastName
    {
        get
        {
            return this._lastName;
        }
        set
        {
            this._lastName = value;
        }
    }
    public virtual string FullName
    {
        get
        {
            return this._fullName;
        }
        set
        {
            this._fullName = value;
        }
    }
}

Thank you, Stephen

like image 467
Stephen Patten Avatar asked Jan 24 '26 09:01

Stephen Patten


1 Answers

After pulling the source and adding the AutoMapper.Net4 project to the console I was able to diagnose the issue.

The API introduced when Jimmy removed the Static version and then re-added it back through me off guard, the syntax is just a bit different now with the new API, anyways. Below is the exception when the source was added, notice the difference between this and what was originally throw via Nuget?

throw new InvalidOperationException("Mapper not initialized. Call Initialize with appropriate configuration."); 

This lead me right back to the Getting Started Docs on GitHub, where I soon discovered that I hadn't initialized the mapper like so

var mapper = config.CreateMapper();  

then instead of the static Mapper

Cutomer2 entity = Mapper.Map<Customer, Cutomer2>(request);

you use the IMapper interface from above like so

Cutomer2 entity = mapper.Map<Customer, Cutomer2>(request);

Problem solved. Hope this helps

like image 174
Stephen Patten Avatar answered Jan 27 '26 00:01

Stephen Patten



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!