【发布时间】:2013-12-17 09:48:42
【问题描述】:
我遇到了一些自动映射器问题。
将Mapper.AssertConfigurationIsValid(); 添加到我的代码后,我收到以下错误:
The following property on CollectiveDistributedPolling.AnswerDto cannot be mapped:
Answer
Add a custom mapping expression, ignore, add a custom resolver, or modify the destination type CollectiveDistributedPolling.AnswerDto.
Context:
Mapping to property Answer from System.Int32 to CollectiveDistributedPolling.AnswerDto
Mapping to property QuestionAnswer from CollectiveDistributedPolling.QuestionAnswer to CollectiveDistributedPolling.QuestionAnswerDto
Mapping to property QuestionAnswer from System.Collections.Generic.ICollection`1[[CollectiveDistributedPolling.QuestionAnswer, CollectiveDistributedPolling, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] to System.Collections.Generic.List`1[[CollectiveDistributedPolling.QuestionAnswerDto, CollectiveDistributedPolling, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]
Mapping from type CollectiveDistributedPolling.Question to CollectiveDistributedPolling.QuestionDto
Exception of type 'AutoMapper.AutoMapperConfigurationException' was thrown.
所以有一个错误将答案映射到 AnswerDto,但映射应该是直截了当的(见下文)
[DataContract]
public class AnswerDto
{
[DataMember]
public int ID { get; set; }
[DataMember]
public string answer { get; set; }
[DataMember]
public List<QuestionAnswerDto> QuestionAnswer { get; set; }
[DataMember]
public List<UserAnswerDto> UserAnswer { get; set; }
}
public partial class Answer
{
public Answer()
{
this.QuestionAnswer = new HashSet<QuestionAnswer>();
this.UserAnswer = new HashSet<UserAnswer>();
}
public int ID { get; set; }
public string answer { get; set; }
public virtual ICollection<QuestionAnswer> QuestionAnswer { get; set; }
public virtual ICollection<UserAnswer> UserAnswer { get; set; }
}
映射
Mapper.CreateMap<Question, QuestionDto>();
Mapper.CreateMap<QuestionDto, Question>();
private Question MapToQuestion(QuestionDto q)
{
return Mapper.Map<QuestionDto, Question>(q);
}
private QuestionDto MapToQuestionDto(Question q)
{
return Mapper.Map<Question, QuestionDto>(q); <<<< ERROR HERE
}
这是问题的SQL 表。如您所见,从 Question(next) 到 Question(ID) 存在外键约束。
这是我的 Model.edmx
的一部分如果您还有其他问题,请提出。
【问题讨论】:
-
Next也是QuestionDto对象吗?另外,您的对象的数据类型是什么?Next肯定有问题,但错误意味着 AutoMapper 正在尝试将int映射到QuestionDto。只是看一下图表,但原因并不完全清楚。 -
下一个是
Question对象。我将发布有关该对象的更多信息。 -
另外,我不知道为什么它试图将
int映射到QuestionDto
标签: c# wcf linq-to-sql automapper