title: Mapping DTO back to model with AutoMapper
body: <p>I have a Fixture model :</p>
  
  <pre><code>public partial class Fixture
  {
      public int FixtureId { get; set; }
      public string Season { get; set; }
      public byte Week { get; set; }
  
      //foreign key
      public int AwayTeamId { get; set; }
      //navigation properties
      public virtual Team AwayTeam { get; set; }
  
      //foreign key
      public int HomeTeamId { get; set; }
      //navigation properties
      public virtual Team HomeTeam { get; set; }
  
      public byte? AwayTeamScore { get; set; }
      public byte? HomeTeamScore { get; set; }
  }
  </code></pre>
  
  <p>And a Fixture DTO :</p>
  
  <pre><code>public class FixtureDTO
  {
      public int Id { get; set; }
      public string Season { get; set; }
      public byte Week { get; set; }
      public string AwayTeamName { get; set; }
      public string HomeTeamName { get; set; }
      public byte? AwayTeamScore { get; set; }
      public byte? HomeTeamScore { get; set; }
  }
  </code></pre>
  
  <p>I am using AutoMapper for the mapping and this is my first attempt using it.  Here is my mapping :</p>
  
  <pre><code>CreateMap&lt;Fixture, FixtureDTO&gt;()
              .ForMember(dest =&gt; dest.Id, opt =&gt; opt.MapFrom(src =&gt; src.FixtureId))
              .ForMember(dest =&gt; dest.AwayTeamName, opt =&gt; opt.MapFrom(src =&gt; src.AwayTeam.TeamName))
              .ForMember(dest =&gt; dest.HomeTeamName, opt =&gt; opt.MapFrom(src =&gt; src.HomeTeam.TeamName));
          CreateMap&lt;FixtureDTO, Fixture&gt;();
  </code></pre>
  
  <p>It works fine in taking the Fixture and mapping it to the FixtureDTO which I use to display the data.  But when I want to update the data and pass the FixtureDTO back to map it back to Fixture I get an error.</p>
  
  <pre><code>public HttpResponseMessage PutFixture(int id, FixtureDTO fixture)
      {
          if (ModelState.IsValid &amp;&amp; id == fixture.Id)
          {
              //do mapping manually here?
  
              var updated = _repository.UpdateFixture(Mapper.Map&lt;Fixture&gt;(fixture));
              return Request.CreateResponse(updated ? HttpStatusCode.OK : HttpStatusCode.NotFound);
          }
          return Request.CreateResponse(HttpStatusCode.BadRequest);
      }
  </code></pre>
  
  <p>This is the error I get :</p>
  
  <pre><code>Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded. Refresh ObjectStateManager entries.
  </code></pre>
  
  <p>Can anybody help with this?</p>
  
  <p>EDIT : Reverse mapping :</p>
  
  <pre><code>CreateMap&lt;Fixture, FixtureDTO&gt;()
              .ForMember(dest =&gt; dest.Id, opt =&gt; opt.MapFrom(src =&gt; src.FixtureId))
              .ForMember(dest =&gt; dest.AwayTeamName, opt =&gt; opt.MapFrom(src =&gt; src.AwayTeam.TeamName))
              .ForMember(dest =&gt; dest.HomeTeamName, opt =&gt; opt.MapFrom(src =&gt; src.HomeTeam.TeamName));
          CreateMap&lt;FixtureDTO, Fixture&gt;()
              .ForMember(dest =&gt; dest.FixtureId, opt =&gt; opt.MapFrom(src =&gt; src.Id))
              .ForMember(dest =&gt; dest.AwayTeam.TeamName, opt =&gt; opt.MapFrom(src =&gt; src.AwayTeamName))
              .ForMember(dest =&gt; dest.HomeTeam.TeamName, opt =&gt; opt.MapFrom(src =&gt; src.HomeTeamName));
  </code></pre>