缺少类型映射配置或不支持的映射 [英] Missing type map configuration or unsupported mapping

查看:756
本文介绍了缺少类型映射配置或不支持的映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请问有人解释这个错误是什么意思吗?我曾经使用过automapper一次,但从未遇到过此类错误.

Could somebody please explain what this error means? I have used automapper onces before but never had this kind of error.

错误

服务器在处理请求时遇到错误.异常消息为缺少类型映射配置或不支持的映射.映射类型:Char-> QuestionDto System.Char-> CollectiveDistributedPolling.QuestionDto目标路径:QuestionDto.Question1.Question1.Question10 [0]源值:R'.

The server encountered an error processing the request. The exception message is 'Missing type map configuration or unsupported mapping. Mapping types: Char -> QuestionDto System.Char -> CollectiveDistributedPolling.QuestionDto Destination path: QuestionDto.Question1.Question1.Question10[0] Source value: 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,我可以指出问题所在.与

Thanks to danludwig I could pinpoint the problem. It has something to do with

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

但是这种映射对我来说似乎很好

But that mapping seems fine to me

推荐答案

这基本上意味着AutoMapper缺少有关如何从一个属性映射到另一个属性的信息.

This basically means AutoMapper is missing information about how to map from one property to another.

尽管通过查看错误无法分辨,但是您可以尝试以下过程来找出缺少映射的属性.首先忽略目标类型的所有属性:

Though I can't tell by looking at the error, you can try the following process to figure out which property is missing the mapping. Start out by ignoring all of your destination type's properties:

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无法确定如何映射的属性.我怀疑是在您的收藏属性之一中...

Then, one by one, uncomment the ForMember lines until your exception is raised again. That is the property AM can't figure out how to map. I suspect is is in one of your collection properties...

更新

我想知道这里是否存在递归问题.试试这个:

I'm wondering if perhaps there is a recursion problem going on here. Try this:

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

这里不是说您有数据问题,但是如果这样做了,您应该期望AM抛出.如果您是question.Next == question,我想AM会一遍又一遍地尝试将属性映射到其所有者的堆栈溢出.

Not saying you have a data problem here, but if you did, you should expect AM to throw. If your question.Next == question, I imagine that AM would overflow the stack trying to map a property to it's owner over and over again.

这篇关于缺少类型映射配置或不支持的映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆