title: Update query needs that all related items to be deleted and re-inserted, which degrades the speed of my application
body: <p>I have the database structure like below:</p>
  
  <p><a href="https://i.stack.imgur.com/dEU8T.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/dEU8T.jpg" alt="enter image description here"></a></p>
  
  <p>When I update order, I use the following query:</p>
  
  <pre><code>using (var xaction = new TransactionScope())
  {
      foreach (OrderItemDetail orderItemDetail in OrderItemDetailClient.GetAllOrderItemDetails())
      {
          if (orderItemDetail.OrderId == NewOrder.OrderId)
          {
              OrderItemDetailClient.DeleteOrderItemDetail(orderItemDetail);
          }
      }
  
      IEnumerable&lt;Dispatch&gt; dispatches = DispatchClient.GetAllDispatches().Where(x =&gt; x.OrderId == NewOrder.OrderId);
      foreach (Dispatch dispatch in dispatches)
      {
          foreach (DispatchItemDetail dispatchItemDetail in DispatchItemDetailClient.GetAllDispatchItemDetails().Where(x =&gt; x.InvoiceId == dispatch.InvoiceId))
          {
              DispatchItemDetailClient.DeleteDispatchItemDetail(dispatchItemDetail);
          }
  
          DispatchClient.DeleteDispatch(dispatch);
      }
  
      OrderClient.UpdateOrder(NewOrder);
  
      xaction.Complete();
      TransactionSucceded = true;
  }
  </code></pre>
  
  <p>The UpdateOrder method in OrderClient looks like:</p>
  
  <pre><code>public Order UpdateOrder(Order Order)
  {
      IOrderRepository OrderRepository = _DataRepositoryFactory.GetDataRepository&lt;IOrderRepository&gt;();
  
      Order updatedEntity = null;
  
      if (Order.OrderId == 0)
      {
          updatedEntity = OrderRepository.Add(Order);
      }
      else
      {
          updatedEntity = OrderRepository.Update(Order);
      }
  
      return updatedEntity;
  
  }
  </code></pre>
  
  <p>And in OrderRepository:</p>
  
  <pre><code>protected override Order UpdateEntity(RateDifferenceContext entityContext, Order entity)
  {
      return (from e in entityContext.OrderSet
              where e.OrderId == entity.OrderId
              select e).FirstOrDefault();
  }
  </code></pre>
  
  <p>And then in DataRepositoryBase class I am using the below method:</p>
  
  <pre><code>public T Update(T entity)
  {
      using (U entityContext = new U())
      {
          T existingEntity = UpdateEntity(entityContext, entity);
          SimpleMapper.PropertyMap(entity, existingEntity);
          entityContext.SaveChanges();
          return existingEntity;
      }
  }
  </code></pre>
  
  <p>When deleting the related entities, I get an error: <code>The relationship could not be changed because one or more of the foreign-key properties is non-nullable</code></p>
  
  <p>I know that manually I don't need to delete orderItemDetails, Dispatches and DispatchItemDetails before saving the order. But If I don't do that then I get error mentioned below:</p>
  
  <pre><code>Multiplicity constraint violated
  </code></pre>
  
  <p>I am really new to entity-framework. Can anybody suggest me a good way to save the order? So that I can get speed.</p>
  
  <p><strong>Update:</strong></p>
  
  <blockquote>
    <p>Where does NewOrder come from? Is it really new?</p>
  </blockquote>
  
  <p><code>NewOrder</code> is a property of type <code>Order</code>, which is initialized in the Constructor of the ViewModel. As well as when you edit or save. I mean If user is creating a new order than <code>NewOrder</code> is new <code>Order</code>. And if user is editing an existing order, than <code>NewOrder</code> is an <code>existing Order</code>.</p>
  
  <blockquote>
    <p>What is OrderItemDetailClient. What is DispatchClient?</p>
  </blockquote>
  
  <p><code>OrderItemDetailClient</code> is a variable of type <code>IOrderItemDetailService</code>, which is also initialized in the Constructor of ViewModel as follows:</p>
  
  <pre><code>OrderItemDetailClient = serviceFactory.CreateClient&lt;IOrderItemDetailService&gt;();
  </code></pre>
  
  <p>which inturn implies that <code>IOrderItemDetailService</code> is the WCF service implemeted class.</p>
  
  <p>Similarly, </p>
  
  <p><code>DispatchClient</code> is a variable of type <code>IDispatchService</code>, which is also initialized in the Constructor of ViewModel as follows:</p>
  
  <pre><code>DispatchClient = serviceFactory.CreateClient&lt;IDispatchService&gt;();
  </code></pre>
  
  <p>which inturn implies that <code>IDispatchService</code> is the WCF service implemeted class.</p>
  
  <blockquote>
    <p>What's the role of orderItemDetailList, TotalQuantity, and TotalAmount?</p>
  </blockquote>
  
  <p>Sorry, that code was not required. I think I forgot to delete that.</p>
  
  <blockquote>
    <p>Why do you delete ( and not add) Dispatches?</p>
  </blockquote>
  
  <p>Update order automatically saves the Order, OrderItemDetail, Dispatch and its related DispatchItemDetail.</p>
  
  <p>So, If I do not delete the existing Dispatches of the NewOrder, then when I update the Order, the Cascade update or something else thinks that I already have a Dispatch with the primary key, that I am now trying to store. So, I get an error : </p>
  
  <pre><code>Multiplicity constraint violated. The role '…' of the 
          relationship '…' has multiplicity 1 or 0..1
  </code></pre>
  
  <blockquote>
    <p>The most pressing question is: why don't you just add the items that are new, delete the ones that are removed and leave the rest alone?</p>
  </blockquote>
  
  <p>I think I answered this question in the above answer, that If I do that I get <code>Multiplicity constraint violated</code> error when saving the updates.</p>