title: Can you get the DbContext from a DbSet?
body: <p>In my application it is sometimes necessary to save 10,000 or more rows to the database in one operation. I've found that simply iterating and adding each item one at a time can take upwards of half an hour.</p>
  
  <p>However, if I disable AutoDetectChangesEnabled it takes ~ 5 seconds (which is exactly what I want)</p>
  
  <p>I'm trying to make an extension method called "AddRange" to DbSet which will disable AutoDetectChangesEnabled and then re-enable it upon completion.</p>
  
  <pre><code>public static void AddRange&lt;TEntity&gt;(this DbSet&lt;TEntity&gt; set, DbContext con, IEnumerable&lt;TEntity&gt; items) where TEntity : class
      {
          // Disable auto detect changes for speed
          var detectChanges = con.Configuration.AutoDetectChangesEnabled;
          try
          {
              con.Configuration.AutoDetectChangesEnabled = false;
  
              foreach (var item in items)
              {
                  set.Add(item);
              }
          }
          finally
          {
              con.Configuration.AutoDetectChangesEnabled = detectChanges;
          }
      }
  </code></pre>
  
  <p>So, my question is: Is there a way to get the DbContext from a DbSet? I don't like making it a parameter - It feels like it should be unnecessary.</p>