简体   繁体   中英

Why won't this AutoMapper configuration map properly?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM