title: Dapper insert or Update?
body: <p>I haven't started using Dapper just yet but just stumbled across it today as I was researching about bulk insert/updates. Currently I'm using EF6 but I would like to look at using Dapper in the future of my bulk stuff. For this app there could be ~15k records but I have other apps that could be ~100k records.</p>
  
  <p>I'm trying to research a way that I could convert the following EF code into Dapper. All it does is reads in a record from a file, look to see if that employee exists in the DB, if so it updates the properties with the values from the file and if not it creates a new object with the values from the file. </p>
  
  <p>I couldn't find any examples when I was looking around. All I could find was how to do a simple insert or an update. I didn't really find a good examples of bulk insert/update. It's very possible I'm just not understanding how to use Dapper just yet.  </p>
  
  <p>How would I do this with Dapper?</p>
  
  <pre><code>int count = 1;
  using (ctx = new DataContext())
  {
      ctx.Configuration.AutoDetectChangesEnabled = false;      
      ctx.Configuration.ValidateOnSaveEnabled = false;
  
      while ((record = srFile.ReadLine()) != null)
      {
          int employeeId = int.Parse(record.Substring(2, 8));
  
          bio_employee employee = ctx.bio_employee.FirstOrDefault(e =&gt; e.emp_id == employeeId);
  
          if (employee != null)
          {
              SetEmployeeData(employee, record);
  
              ctx.Entry(employee).State = System.Data.Entity.EntityState.Modified;
          }
          else
          {
              employee = new bio_employee();
              employee.emp_id = employeeId;
  
              SetEmployeeData(employee, record);
  
              ctx.bio_employee.Add(employee);
          }
  
  
          if (count % batchSize == 0)
          {
              ctx.SaveChanges();
              ctx.Dispose();
              ctx = new DataContext();
          }
  
          count++;
      }
      ctx.SaveChanges();      //save any remaining
  }
  </code></pre>