2013-12-16 86 views
0

有人能解釋這個錯誤的含義嗎?我以前使用過automapper,但從未發生過這種錯誤。缺少類型映射配置或不支持的映射

錯誤

服務器遇到錯誤處理請求。異常消息是'丟失類型映射配置或不支持的映射。映射類型:Char - > QuestionDto System.Char - > CollectiveDistributedPolling.QuestionDto目標路徑:QuestionDto.Question1.Question1.Question10 [0]源值:R'。

Service1.svc.cs

public Service1() { 
    Mapper.CreateMap<Question, QuestionDto>(); 
    Mapper.CreateMap<QuestionDto, Question>(); 
} 

private Question MapToQuestion(QuestionDto q) 
{ 
     return Mapper.Map<QuestionDto, Question>(q); 
} 

private QuestionDto MapToQuestionDto(Question q) <<< EXCEPTION GETS THROWN HERE 
{ 
     return Mapper.Map<Question, QuestionDto>(q); 
} 

public QuestionDto ThrowQuestion(string user) 
{ 
     return MapToQuestionDto(Database.GetInstance().ThrowQuestion(user)); 
} 

IService1.cs

[OperationContract] 
     [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json)] 
     QuestionDto ThrowQuestion(String user); 

[DataContract] 
    public class QuestionDto 
    { 
     [DataMember] 
     public int ID { get; set; } 
     [DataMember] 
     public int next { get; set; } 
     [DataMember] 
     public String question { get; set; } 
     [DataMember] 
     public ICollection<QuestionDto> QuestionPhrase { get; set; } 
     [DataMember] 
     public QuestionDto Next{ get; set; } 
     [DataMember] 
     public ICollection<QuestionAnswerDto> QuestionAnswer { get; set; } 
     [DataMember] 
     public ICollection<UserAnswerDto> UserAnswer { get; set; } 
    } 

Question.cs

public partial class Question 
    { 
     public Question() 
     { 
      this.QuestionPhrase = new HashSet<Question>(); 
      this.QuestionAnswer = new HashSet<QuestionAnswer>(); 
      this.UserAnswer = new HashSet<UserAnswer>(); 
     } 

     public int ID { get; set; } 
     public string question { get; set; } 
     public Nullable<int> next { get; set; } 

     public virtual ICollection<Question> QuestionPhrase { get; set; } 
     public virtual Question Next { get; set; } 
     public virtual ICollection<QuestionAnswer> QuestionAnswer { get; set; } 
     public virtual ICollection<UserAnswer> UserAnswer { get; set; } 
    } 
} 

由於d anludwig我可以找出問題所在。它是與

[DataMember] 
public QuestionDto Next{ get; set; } 

但是,這似乎映射罰款我

+0

請發表您的問題類。 「char」類型的任何一個對象是否有任何屬性? – danludwig

+0

@danludwig發佈了Question類,但沒有字符類型 – David

+0

您可以使用Mapper.DynamicMap <>進行檢查。 –

回答

4

這基本上意味着AutoMapper缺少有關如何映射從一個屬性到另一個信息。

儘管通過查看錯誤無法判斷,但您可以嘗試以下過程來確定哪些屬性缺少映射。忽略所有的目標類型屬性的開始了:

Mapper.CreateMap<Question, QuestionDto>() 
    .ForMember(d => d.ID, o => o.Ignore()) 
    .ForMember(d => d.next, o => o.Ignore()) 
    .ForMember(d => d.question, o => o.Ignore()) 
    .ForMember(d => d.QuestionPhrase, o => o.Ignore()) 
    .ForMember(d => d.Next, o => o.Ignore()) 
    .ForMember(d => d.QuestionAnswer, o => o.Ignore()) 
    .ForMember(d => d.UserAnswer, o => o.Ignore()) 
    // also ignore any other properties 
; 

然後,一個接一個,取消對ForMember行,直到你的異常再次升高。 AM是無法弄清楚如何映射的屬性。我懷疑是在您的收藏屬性之一...

更新

我不知道是否有可能是一個遞歸問題怎麼回事。試試這個:

.ForMember(d => d.Next, o => o.ResolveUsing(s => { 
    var tryToMap = Mapper.Map<QuestionDto>(s.Next); // exception here??? 
    return null; 
})); 

並不是說你在這裏有數據問題,但是如果你這樣做了,你應該預期AM會拋出。如果你的question.Next == question,我想象AM會溢出堆棧試圖將一個屬性映射到它的所有者一遍又一遍。

+0

偉大的提示! 我不得不改變 '[DataMember] public Question Next {get;組; }' 到 '[DataMember] public QuestionDto Next {get;組; }' – David

+0

錯誤再次啓動。 我跑你的「測試」,它肯定'.ForMember(d => d.Next,o => o。忽略())' – David

+1

好吧,它是一樣的確切的錯誤,還是有點不同?我想知道AM是否會將嵌套引用映射到同一類型。你可能需要一個自定義的ResolveUsing()委託。 'Next'不會引用'Question'的同一個實例,是嗎? – danludwig

相關問題