title: Method that adds elements to a DbSet from multiple threads
body: <pre><code>static Object LockEx=new Object();
  public void SaveMyData(IEnumerable&lt;MyData&gt; list)
      {
          lock (LockEx)
          {
              using (PersistencyContext db = new PersistencyContext())
              {
                  foreach (var el in list)
                  {
                      try
                      {
                          db.MyData.Add(el);
                          db.SaveChanges();
                      }
                      catch (DbUpdateException)
                      {
                          db.Entry(el).State = EntityState.Modified;
                          db.SaveChanges();
                      }
                  }
              }
          }
  
      }
  </code></pre>
  
  <p>This methods is called from multiple threads. Right now I use a static lock to avoid 2 threads to save data at the same time. Though this is wrong because I only want to save data. The catch is used to create an update query in case the insert (Add) fails because the entry already exists.</p>
  
  <p>What happens if I remove the lock. How will the <code>SaveChanges</code> work? How should my code look like? Thanks</p>