title: EF6 - Is there a way to clear EF cache / Local items without causing any change into the database?
body: <p>Basically I am mixing EF with one call to a stored procedure which does some batch deletions, otherwise EF is too slow.</p>
  
  <p>Here is some pseudo-code of this scenario (I have more complex code in reality):</p>
  
  <pre><code>public void RemoveCustomer(int customerUID)
  {
     // this code is running in one db transaction
     {
        // retrieve certain orders of particular customer using EF
        var orders = repoOrders.GetOrdersOfCustomer(filter, customerUID);
  
        // do something with above orders before deletion using EF
        repoX.DoSomethingWithOrders(orders);
  
        // call SP to delete all orders of customer 
        repoOrders.DeleteAllOrdersOfCustomer(customerUID);  // this calls a stored procedure
  
        // delete customer using EF 
        repoCustomers.DeleteCustomer(customerUID);  // throws exception due to relationship!
     }
  }
  </code></pre>
  
  <p>of course, customers-orders is <code>one-to-many</code> (1:m) relation.</p>
  
  <p>I want to avoid in the above scenario the exception which is thrown when there will be some orders loaded by the context belonging to the customer that gets deleted. Exception is: </p>
  
  <blockquote>
    <p>"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>So, I want to know if is possible to clear some/all orders from <code>&lt;DbSet&gt;.Local</code> without causing any change into the database after calling the stored procedure and before user gets deleted. </p>
  
  <p>I guess <a href="http://msdn.microsoft.com/en-us/library/system.data.objects.objectcontext.detach%28v=vs.110%29.aspx" rel="noreferrer">Detach</a> could be used, but this means I should loop through.</p>
  
  <p>What would you recommend?</p>
  
  <p><strong>Edit:</strong> I am new to EF and I am integrating EF now after the repositories were done using ADO.NET, and yes, the BL has been kept the same... so I try this integration with minimal efforts at this point.</p>
  
  <p><strong>Note:</strong> I can't make changes on the database structure.</p>