缺少类型映射配置或不支持的映射

时间:2013-12-16 18:59:03

标签: c# json wcf linq-to-sql 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; }
    }
}

感谢danludwig,我可以找出问题所在。它与

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

但是那个映射对我来说似乎很好

1 个答案:

答案 0 :(得分:5)

这基本上意味着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会溢出堆栈,试图一次又一次地将一个属性映射到它的所有者。