2017-05-25 72 views
1

我在HuntMemberBLL类中具有以下构造函数。如何使Automapper选择无参数构造函数

// Default Constructor 
    public HuntMemberBLL() 
    {IsNew = true;} 

    // Get Constructor 
    public HuntMemberBLL(long HuntMemberID) 
    { //DbLoading 
     IsNew = false; 
    } 

而且我有一个没有任何构造函数的HuntMemberDTO类。问题是当我尝试映射到HuntMemberBLL类时,Automapper选择了Get构造函数而不是Default构造函数。有什么办法让它使用默认的构造函数吗?

回答

1

您可以通过ConstructUsing方法指定要使用的构造函数。例如像:

Mapper.Initialize(config => 
config.CreateMap<HuntMemberDTO, HuntMemberBLL>() 
     .ConstructUsing((Func<HuntMemberDTO, HuntMemberBLL>)(x => new HuntMemberBLL())); 
+0

我已经试过了,它给了我一个错误说“的号召是以下方法或属性之间暧昧:“AutoMapper.IMappingExpression .ConstructUsing(System.Func )'和'AutoMapper.IMappingExpression .ConstructUsing(System.Func <.. HuntMemberDTO,HuntMemberBLL>)“ – sandeep

+0

AutoMapper需要类型信息。你可以尝试一下我现在编辑我的代码的方式吗? – gpanagopoulos

+0

谢谢,它的工作。 – sandeep