自动映射器在同一个表中使用外键时遇到问题

时间:2013-12-17 09:48:42

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

我遇到了一些自动播放器问题。

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表。如您所见,问题(下一个)与问题(ID)之间存在外键约束。

SQL table of Question

这是我的 Model.edmx

的一部分

Model.edmx

如果您还有其他问题,请询问。

1 个答案:

答案 0 :(得分:1)

看起来EF与DTO一起工作的方式可能是抛出AutoMapper的大头钉。请记住,使用AutoMapper,您可以在逐个属性的基础上准确指定要执行的操作。

例如,离开this question,您可以尝试这样的事情:

// target = QuestionDto, source = Question
Mapper.CreateMap<Question, QuestionDto>()
      .ForMember(target => target.ID, options => options.MapFrom(source => source.ID));

您还可以告诉AutoMapper忽略Next并在映射后直接处理它:

 Mapper.CreateMap<Question, QuestionDto>()
       .ForMember(target => target.Next, options => options.Ignore())
       .AfterMap(source, target => {
           // do some magic here
           // source = Question, target = QuestionDto
       });

或者,您可以完全切换齿轮并尝试关注the answer to this question,建议使用ValueInjecter展平/取消展开DTO而不是自动映射:

  

使用http://valueinjecter.codeplex.com/,它会扁平化和不平整,以及您需要的任何其他内容,下载中有一个asp.net mvc示例应用程序,其中演示了所有功能(也是单元测试)