2013-12-16 83 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

相关问题