C# 为什么赢了';这个自动映射器配置映射是否正确?

C# 为什么赢了';这个自动映射器配置映射是否正确?,c#,automapper,C#,Automapper,鉴于下面的代码,为什么我在映射阶段总是遇到异常?2个DTO真的那么不同吗?下面是引发异常的符号服务器pdb的代码行 throw new AutoMapperMappingException(context, "Missing type map configuration or unsupported mapping."); 真正让我头疼的是@jbogard在异常处理和插装方面做得非常出色。关于AutoMapper,源对象和目标对象都有大量的上下文数据,以及抛出异常时映射器的状态。我仍然无法理解

鉴于下面的代码,为什么我在映射阶段总是遇到异常?2个DTO真的那么不同吗?下面是引发异常的符号服务器pdb的代码行

throw new AutoMapperMappingException(context, "Missing type map configuration or unsupported mapping.");
真正让我头疼的是@jbogard在异常处理和插装方面做得非常出色。关于AutoMapper,源对象和目标对象都有大量的上下文数据,以及抛出异常时映射器的状态。我仍然无法理解

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;
        }
    }
}
void Main()
{
var config=new-MapperConfiguration(cfg=>
{
cfg.ShouldMapProperty=p=>p.GetMethod.IsPublic | | p.GetMethod.IsVirtual;
cfg.CreateMap()
.ReverseMap();
});
config.assertconfigurationsvalid();
客户请求=新客户
{
FirstName=“你好”,LastName=“世界”
};
request.FullName=request.FirstName+“”+request.LastName;
Customer2 entity=Mapper.Map(请求);
控制台。写入线(“完成”);
}
公共类客户
{
公共字符串名{get;set;}
公共字符串LastName{get;set;}
公共字符串全名{get;set;}
}
[可序列化()]
公共部分类Customer2
{
私有字符串_firstName;
私有字符串_lastName;
私有字符串\u全名;
公共虚拟字符串名
{
得到
{
返回此。\u firstName;
}
设置
{
这个。_firstName=value;
}
}
公共虚拟字符串LastName
{
得到
{
返回此。\u lastName;
}
设置
{
这是。_lastName=value;
}
}
公共虚拟字符串全名
{
得到
{
返回此。\u全名;
}
设置
{
这是。_fullName=value;
}
}
}
谢谢,,
Stephen

在提取源代码并将AutoMapper.Net4项目添加到控制台后,我能够诊断问题

Jimmy删除了静态版本,然后通过我毫无防备地重新添加它时引入的API,无论如何,新API的语法现在有点不同。下面是添加源代码时的例外情况,请注意这与最初通过Nuget抛出的内容之间的区别

throw new InvalidOperationException("Mapper not initialized. Call Initialize with appropriate configuration."); 
这让我回到了GitHub上,我很快发现我没有像这样初始化映射器

var mapper = config.CreateMapper();  
然后代替静态映射器

Cutomer2 entity = Mapper.Map<Customer, Cutomer2>(request);
Cutomer2 entity=Mapper.Map(请求);
您可以像这样从上面使用IMapper接口

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

问题解决了。希望这有帮助

引发异常的详细信息是什么?
Customer2
标记为
partial
,类的其余部分在哪里?您也可以只使用Mapper。如果要使用静态Mapper类,请初始化。