【问题标题】:Missing type map configuration or unsupported mapping缺少类型映射配置或不支持的映射
【发布时间】:2013-12-16 18:59:02
【问题描述】:

有人能解释一下这个错误是什么意思吗?我以前用过一次自动映射器,但从来没有出现过这种错误。

错误

服务器在处理请求时遇到错误。异常消息是“缺少类型映射配置或不支持的映射。映射类型: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; } 

但我觉得这个映射很好

【问题讨论】:

  • 请发布您的Question 课程。 char 类型的任一对象是否有任何属性?
  • @danludwig 发布了 Question 类,但里面没有 char 类型
  • 你可以用 Mapper.DynamicMap 代替。

标签: c# json wcf linq-to-sql automapper


【解决方案1】:

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

【讨论】:

  • 很棒的提示!我不得不将[DataMember] public Question Next{ get; set; } 更改为[DataMember] public QuestionDto Next{ get; set; }
  • 错误又开始了。我运行了你的“测试”,它肯定是.ForMember(d =&gt; d.Next, o =&gt; o.Ignore())
  • 好的,这是完全相同的错误,还是有点不同?我想知道 AM 是否可能在将嵌套引用映射到同一类型时遇到问题。您可能需要一个自定义 ResolveUsing() 委托。 Next 不会引用 Question 的同一个实例,是吗?
  • Question(next) 有一个 Question(ID) 的外键,LINQ 生成了 2 个引用对象 QuestionPhase 和 Next,我真的不明白为什么......其中一个可能是引用同一个实例就像你建议的那样。我可以在 Model.edmx 中删除一个吗?
  • 在不了解您的数据或业务领域的情况下,我不会告诉您要删除什么。特别是因为我不将 EF 与 edmx 文件一起使用,至少在过去 4 年左右。
猜你喜欢
  • 2015-06-27
  • 2019-03-01
  • 2017-04-16
  • 2015-09-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-26
  • 1970-01-01
相关资源
最近更新 更多