Note that there are some explanatory texts on larger screens.

plurals
  1. POAutomapper having trouble with foreign key to the same table
    primarykey
    data
    text
    <p>I am having some automapper issues. </p> <p>After adding <code>Mapper.AssertConfigurationIsValid();</code> to my code I get the following error:</p> <pre><code>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. </code></pre> <p>So there is an error mapping Answer to AnswerDto, but the mapping should be straight forward (see below)</p> <pre><code>[DataContract] public class AnswerDto { [DataMember] public int ID { get; set; } [DataMember] public string answer { get; set; } [DataMember] public List&lt;QuestionAnswerDto&gt; QuestionAnswer { get; set; } [DataMember] public List&lt;UserAnswerDto&gt; UserAnswer { get; set; } } public partial class Answer { public Answer() { this.QuestionAnswer = new HashSet&lt;QuestionAnswer&gt;(); this.UserAnswer = new HashSet&lt;UserAnswer&gt;(); } public int ID { get; set; } public string answer { get; set; } public virtual ICollection&lt;QuestionAnswer&gt; QuestionAnswer { get; set; } public virtual ICollection&lt;UserAnswer&gt; UserAnswer { get; set; } } </code></pre> <p><strong>Mappings</strong></p> <pre><code>Mapper.CreateMap&lt;Question, QuestionDto&gt;(); Mapper.CreateMap&lt;QuestionDto, Question&gt;(); private Question MapToQuestion(QuestionDto q) { return Mapper.Map&lt;QuestionDto, Question&gt;(q); } private QuestionDto MapToQuestionDto(Question q) { return Mapper.Map&lt;Question, QuestionDto&gt;(q); &lt;&lt;&lt;&lt; ERROR HERE } </code></pre> <p>Here is the <em>SQL table of Question</em>. As you can see there is a foreign key constraint from Question(next) to Question(ID).</p> <p><img src="https://i.stack.imgur.com/IYyP2.png" alt="SQL table of Question"></p> <p>This is a part of my <em>Model.edmx</em></p> <p><img src="https://i.stack.imgur.com/6Tq2i.png" alt="Model.edmx"></p> <p>If you have any more questions please ask.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload