title: bulk insert and update with ADO.NET Entity Framework
body: <p>I am writing a small application that does a lot of feed processing.  I want to use LINQ EF for this as speed is not an issue, it is a single user app and, in the end, will only be used once a month.</p>
  
  <p>My questions revolves around the best way to do bulk inserts using LINQ EF.</p>
  
  <p>After parsing the incoming data stream I end up with a List of values.  Since the end user may end up trying to import some duplicate data I would like to "clean" the data during insert rather than reading all the records, doing a for loop, rejecting records, then finally importing the remainder.</p>
  
  <p>This is what I am currently doing:  </p>
  
  <pre><code>DateTime minDate = dataTransferObject.Min(c =&gt; c.DoorOpen);
  DateTime maxDate = dataTransferObject.Max(c =&gt; c.DoorOpen);
  
  using (LabUseEntities myEntities = new LabUseEntities())
  {
      var recCheck = myEntities.ImportDoorAccess.Where(a =&gt; a.DoorOpen &gt;= minDate &amp;&amp; a.DoorOpen &lt;= maxDate).ToList();
      if (recCheck.Count &gt; 0)
      {
          foreach (ImportDoorAccess ida in recCheck)
          {
              DoorAudit da = dataTransferObject.Where(a =&gt; a.DoorOpen == ida.DoorOpen &amp;&amp; a.CardNumber == ida.CardNumber).First();
              if (da != null)
                  da.DoInsert = false;
          }
      }
  
      ImportDoorAccess newIDA;
      foreach (DoorAudit newDoorAudit in dataTransferObject)
      {
          if (newDoorAudit.DoInsert)
          {
              newIDA = new ImportDoorAccess
              {
                  CardNumber = newDoorAudit.CardNumber,
                  Door = newDoorAudit.Door,
                  DoorOpen = newDoorAudit.DoorOpen,
                  Imported = newDoorAudit.Imported,
                  RawData = newDoorAudit.RawData,
                  UserName = newDoorAudit.UserName
              };
              myEntities.AddToImportDoorAccess(newIDA);
          }
      }
      myEntities.SaveChanges();
  }
  </code></pre>
  
  <p>I am also getting this error:</p>
  
  <blockquote>
    <p>System.Data.UpdateException was unhandled<br>
     Message="Unable to update the EntitySet 'ImportDoorAccess' because it has a DefiningQuery and no  element exists in the  element to support the current operation."<br>
     Source="System.Data.SqlServerCe.Entity"  </p>
  </blockquote>
  
  <p>What am I doing wrong?</p>
  
  <p>Any pointers are welcome.</p>