title: Prevent the DbContext from repeatedly trying to save bad data
body: <p>I have a process that is importing an Excel Spreadhseet, and parsing the data into my data objects. The source of this data is <strong>very</strong> questionable, as we're moving our customer from a spreadsheet-based data management into a managed database system with checks for valid data.</p>
  
  <p>During my import process, I do some basic sanity checks of the data to accommodate just how bad the data could be that we're importing, but I have my overall validation being done in the DbContext.</p>
  
  <p>Part of what I'm trying to do is that I want to provide the Row # in the spreadsheet that the data is bad so they can easily determine what they need to fix to get the file to import.</p>
  
  <p>Once I have the data from the spreadsheet (<code>model</code>), and the Opportunity they're working with from the database (<code>opp</code>), here's the pseudocode of my process:</p>
  
  <pre><code>foreach (var model in Spreadsheet.Rows) { // Again, pseudocode
      if(opp != null &amp;&amp; ValidateModel(model, opp, row)) {
          // Copy properties to the database object
  
          // This is in a Repository-layer method, not directly in my import process.
          // Just written here for clarity instead of several nested method calls.
          context.SaveChanges(); 
      }
  }
  </code></pre>
  
  <p>I can provide more of the code here if needed, but the problem comes in my DbContext's <code>ValidateEntity()</code> method (override of DbContext). </p>
  
  <p>Again, there's nothing wrong with the code that I've written, as far as I'm aware, but if an Opportunity that failed this level of validation, then it stays as part of the unsaved objects in the <code>context</code>, meaning that it repeatedly tries to get validated every time the <code>ValidateEntity()</code> is called. This leads to a repeat of the same Validation Error message for every row after the initial problem occurs.</p>
  
  <p>Is there a way to [edit]<strong>get the Context to stop trying to validate the object after it fails validation once</strong>[edit]? I know I could wait until the end and call <code>context.SaveChanges()</code> once at the end to get around this, but I would like to be able to match this with what row it is in the Database.</p>
  
  <p>For reference, I am using Entity Framework 6.1 with a Code First approach.</p>
  
  <p><strong>EDIT</strong> Attempting to clarify further for Marc L. (including an update to the code block above)</p>
  
  <p>Right now, my process will iterate through as many rows as there are in the Spreadsheet. The reason why I'm calling my Repository layer with each object to save, instead of working with an approach that only calls <code>context.SaveChanges()</code> once is to allow myself the ability to determine which row is the one that is causing a validation error.</p>
  
  <p>I'm glad that my DbContext's custom <code>ValidateEntity()</code> methods are catching the validation errors, but the problem resides in the fact that it is not throwing the <code>DbEntityValidationException</code> for the same entity multiple times.</p>
  
  <p>I would like it so that if the object fails validation once, the context no longer tries to save the object, regardless of how many times <code>context.SaveChanges()</code> is called.</p>