The operation failed: The relationship could not be changed because one or more of the foreign-key properties is non-nullable.
<p>This question might look like a duplicate to other similar questions. But I suggest you to read the question till end and then decide if its a duplicate of some post or not?????</p>

<p>I have 6 tables in my database as follows:</p>

<p><a href="https://i.stack.imgur.com/7BcFr.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/7BcFr.jpg" alt="enter image description here"></a></p>

<p>I have some records already inserted in all tables.</p>

<p>Now, I am trying to update an Order.</p>

<p>At first, I just tried to Update the order as follows:</p>

<pre><code>CurrentOrder.UpdateOrder(Order);
</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>At this point I got an error saying:</p>

<blockquote>
  <p>Multiplicity constraint violated. The role '…' of the relationship '…' has multiplicity 1 or 0..1</p>
</blockquote>

<p>So, I thought that I need to delete the records which are specific to this order in all the related tables. So, I tried the below code:</p>

<pre><code>using (var xaction = new TransactionScope())
{
    foreach (OrderItemDetail orderItemDetail in OrderItemDetailClient.GetAllOrderItemDetails().Where(x =&gt; x.OrderId == NewOrder.OrderId))
    {
        OrderItemDetailClient.DeleteOrderItemDetail(orderItemDetail);
    }

    foreach (Dispatch dispatch in DispatchClient.GetAllDispatches().Where(x =&gt; x.OrderId == NewOrder.OrderId))
    {
        foreach (DispatchItemDetail dispatchItemDetail in DispatchItemDetailClient.GetAllDispatchItemDetails().Where(x =&gt; x.InvoiceId == dispatch.InvoiceId))
        {
            DispatchItemDetailClient.DeleteDispatchItemDetail(dispatchItemDetail);
        }

        DispatchClient.DeleteDispatch(dispatch);
    }

    OrderClient.UpdateOrder(NewOrder);

    xaction.Complete();
}
</code></pre>

<p>Now, I get another error saying that :</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 get this error on below mentioned line in the last code-block:</p>

<pre><code>DispatchClient.DeleteDispatch(dispatch);
</code></pre>