【问题标题】:Automapper having trouble with foreign key to the same tableAutomapper 在同一张表的外键上遇到问题
【发布时间】: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


【解决方案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,而不是Automapper:

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

【讨论】:

  • 谢谢!我已经尝试过你的第一种方法,但我得到了奇怪的错误Mapper.CreateMap&lt;Question, QuestionDto&gt;() .ForMember(dto =&gt; dto.QuestionAnswer, opt =&gt; opt.MapFrom(q =&gt; Mapper.Map&lt;QuestionAnswer, QuestionAnswerDto&gt;(q.QuestionAnswer)))你知道这行代码有什么问题吗?所以我欠你很多时间!
  • 只要定义了Mapper.CreateMap&lt;QuestionAnswer, QuestionAnswerDto&gt;(),就不必在此处指定MapFrom。 AutoMapper 应该只是这样做。
猜你喜欢
  • 2013-08-28
  • 2019-09-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多