2017-05-25 74 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