title: Use automapper to update an Entity Framework Entity
body: <p>I am trying to use Entity Framework and have Automapper update my entity from my contract.</p>
  
  <p>My code looks like this:</p>
  
  <pre><code>var temp = OrderContract;
  Order order = dataAccess.FindOne&lt;Order&gt;(x =&gt; x.OrderId == temp.OrderId) 
                ?? new Order();
  
  Mapper.Map(OrderContract, order);
  
  if (order.OrderId &lt;= 0)
     dataAccess.Add(order);
  </code></pre>
  
  <p><em>(Note: I am using the Repository Pattern.  dataAccess.FindOne calls CreateQuery to return one entity.)</em> </p>
  
  <p>The problem I am having is with the relationships.  I get this error when I do an update (inserts work just fine):</p>
  
  <blockquote>
    <p>The operation failed: The relationship could not be changed because one or more of the foreign-key properties is non-nullable. When a change is made to a relationship, the related foreign-key property is set to a null value. If the foreign-key does not support null values, a new relationship must be defined, the foreign-key property must be assigned another non-null value, or the unrelated object must be deleted.</p>
  </blockquote>
  
  <p>I am guessing that automapper is not updating the way I want it to.  From the error message and googling around I have surmised that my relationships that are collections (and maybe even the ones that are not collections) are being recreated by Automapper.</p>
  
  <p><strong>How can I tell Automapper to just update and not remake any objects or collections?</strong></p>
  
  <p><strong>Guesses:</strong></p>
  
  <p>I read that maybe I need to use the <code>UseDestinationValue</code> option for automapper.  I went back and put that on all my collections  But when I do that then my inserts fail with a foreign key violation.</p>
  
  <p><strong>Code Mappings:</strong></p>
  
  <p><strong>Using UseDestinationValue only on one collection (this one inserts but will not update):</strong></p>
  
  <pre><code>//+ Source
  Mapper.CreateMap&lt;SourceContract, Source&gt;()
      .IgnoreAllNonExisting();
  
  //+ SelectedRequirement
  Mapper.CreateMap&lt;SelectedRequirementContract, SelectedRequirement&gt;()
      .ForMember(x =&gt; x.SelectedRequirementId, opt =&gt; opt.MapFrom(src =&gt; src.RequirementId))
      .IgnoreAllNonExisting();
  
  //+ Comment Contract
  Mapper.CreateMap&lt;CommentContract, Comment&gt;()
      .ForMember(x =&gt; x.CommentText, opt =&gt; opt.MapFrom(src =&gt; src.Comment))
      .IgnoreAllNonExisting();
  
  //+ Order Automapper setup
  Mapper.CreateMap&lt;OrderContract, Order&gt;()
      .ForMember(x =&gt; x.Source, opt =&gt; opt.MapFrom(src =&gt; src.Source))
      .ForMember(x =&gt; x.Comment, opt =&gt; opt.MapFrom(src =&gt; src.Comment))
      //Although a mapping was created for Comment entity,
      //we still need to map the CommentId of the Order entity otherwise it will remain null during an update.
      //Another way to handle this would be to Delete CommentId from the Order entity.
      //However, if anyone updates (Update from model) OrderDataModel.edmx that property would show up again thus causing
      //a null value to be inserted during an update.
      .ForMember(x =&gt; x.CommentId, opt =&gt; opt.MapFrom(src =&gt; src.Comment.CommentId))
      .ForMember(x =&gt; x.SelectedRequirements, opt =&gt; {opt.UseDestinationValue(); opt.MapFrom(src =&gt; src.Requirements);})
      .ForMember(x =&gt; x.OrderStateId, opt =&gt; opt.MapFrom(src =&gt; src.StateId))
      .ForMember(x =&gt; x.OrderStateId, opt =&gt; opt.MapFrom(src =&gt; src.StateId))
      .IgnoreAllNonExisting();
  </code></pre>
  
  <p><strong>Using UseDestinationValue everywhere (this one does not insert):</strong></p>
  
  <pre><code>//+ Source
  Mapper.CreateMap&lt;SourceContract, Source&gt;()
      .IgnoreAllNonExisting();
  
  //+ SelectedRequirement
  Mapper.CreateMap&lt;SelectedRequirementContract, SelectedRequirement&gt;()
      .ForMember(x =&gt; x.SelectedRequirementId, opt =&gt; { opt.UseDestinationValue(); opt.MapFrom(src =&gt; src.RequirementId); })
      .IgnoreAllNonExisting();
  
  //+ Comment Contract
  Mapper.CreateMap&lt;CommentContract, Comment&gt;()
      .ForMember(x =&gt; x.CommentText, opt =&gt; { opt.UseDestinationValue(); opt.MapFrom(src =&gt; src.Comment); })
      .IgnoreAllNonExisting();
  
  //+ Order Automapper setup
  Mapper.CreateMap&lt;OrderContract, Order&gt;()
      .ForMember(x =&gt; x.Source, opt =&gt; { opt.UseDestinationValue(); opt.MapFrom(src =&gt; src.Source); })
      .ForMember(x =&gt; x.Comment, opt =&gt; { opt.UseDestinationValue(); opt.MapFrom(src =&gt; src.Comment); })
      //Although a mapping was created for Comment entity,
      //we still need to map the CommentId of the Order entity otherwise it will remain null during an update.
      //Another way to handle this would be to Delete CommentId from the Order entity.
      //However, if anyone updates (Update from model) OrderDataModel.edmx that property would show up again thus causing
      //a null value to be inserted during an update.
      .ForMember(x =&gt; x.CommentId, opt =&gt; { opt.UseDestinationValue(); opt.MapFrom(src =&gt; src.Comment.CommentId); })
      .ForMember(x =&gt; x.SelectedRequirements, opt =&gt; { opt.UseDestinationValue(); opt.MapFrom(src =&gt; src.Requirements); })
      .ForMember(x =&gt; x.OrderStateId, opt =&gt; { opt.UseDestinationValue(); opt.MapFrom(src =&gt; src.StateId); })
      .ForMember(x =&gt; x.OrderStateId, opt =&gt; { opt.UseDestinationValue(); opt.MapFrom(src =&gt; src.StateId); })
      .IgnoreAllNonExisting();
  </code></pre>
  
  <p>What do I need to so I can insert and update?</p>