有人可以解释一下这个错误是什么意思吗?我以前用过一次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; }
}
问题.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; }
但是那个映射对我来说似乎没问题
最佳答案
这基本上意味着 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 会溢出堆栈,试图一遍又一遍地将属性映射到它的所有者。
关于c# - 缺少类型映射配置或不支持的映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20618670/