title: Context.SaveChangesAsync() is blocking
body: <p>I am currently rapid prototyping a WPF application following the MVVM pattern and using the Entity Framework and SQL Servre CE.</p>
  
  <p>I am experimenting with the <a href="https://msdn.microsoft.com/en-us/magazine/dn630647.aspx" rel="nofollow noreferrer">AsyncCommand by Stephen Cleary from the MSDN blog</a>.</p>
  
  <p>Everything is working well with the exception of <code>Context.SaveChangesAsync()</code> which is still blocking the UI.</p>
  
  <p>This is the synchronous code which takes about 7 seconds for 10000 "Entities" (a very simple data type):</p>
  
  <pre><code>public int Insert( IEnumerable&lt;Entity&gt; entities )
  {
      using( Context context = new Context( ConnectionString ) )
      {
          context.Entities.AddRange( entities );
          return context.SaveChanges();
      }
  }
  </code></pre>
  
  <p>This is the asynchronous version which is still blocking the UI (I also tried a version without the using statement but that did not help):</p>
  
  <pre><code>public async Task&lt;int&gt; InsertAsync( IEnumerable&lt;Entity&gt; entities )
  {
    using( Context context = new Context( ConnectionString ) )
    {
      context.Entities.AddRange( entities );
      return await context.SaveChangesAsync();
    }
  }
  </code></pre>
  
  <p>The bulk of the work happens in <code>SaveChangesAsync()</code> and wrapping AddRange does not help</p>
  
  <pre><code>await Task.Run( () =&gt; context.Entities.AddRange(entities));
  </code></pre>
  
  <p>Using <code>context.SaveChangesAsync().ConfigureAwait(false)</code> does not help either.</p>
  
  <p>However, if I simply wrap the synchronous version then it is NOT blocking:</p>
  
  <pre><code>public async Task&lt;int&gt; InsertAsync( IEnumerable&lt;Entity&gt; entities )
  {
     return await Task&lt;int&gt;.Run( () =&gt; Insert( entities ) ); 
  }
  </code></pre>
  
  <p>This code is called from the ViewModel using an <code>AsyncCommand</code> (see link above for the MSDN article):</p>
  
  <pre><code>CreateRandomEntitiesAsyncCommand = AsyncCommand.Create( 
      token =&gt; CreateRandomEntitiesAsync(token));
  
  public IAsyncCommand CreateRandomPatientsAsyncCommand { get; private set;  }
  
  public async Task CreateRandomAsync()
  {
     int numberToGenerate = 10000;
  
     // Get all existing unique ids from the data layer, this does not block
     IEnumerable&lt;string&gt; existingUniqueIDs = await dataLayer.GetUniqueIDsNoTrackingAsync();
  
     // randomly create 10000 entities, ensuring they are unique, does not block either
     Entity[] entities = await Task.Run( () =&gt; 
       CreateEntities(numberToGenerate, existingUniqueIDs));
  
     // this does block, see above
     await dataLayer.InsertAsync( entities );
  
     // ...
     // Re-fetch all entities from the db and issue PropertyChanged 
     // for the Entities property to refresh the UI.
     // The code above is still blocking if this is commented out.
  }
  </code></pre>
  
  <p>What am I making wrong?</p>
  
  <p><strong>Intermediate update</strong></p>
  
  <p>It seems this issue is related to SQL Server CE and its "underlying implementation" possibly mapping the asynchronous Entity Framework code to synchronous code so that it is executed on the UI thread and thus blocking the UI.</p>
  
  <p>Also see: <a href="https://stackoverflow.com/a/28690405/5851786">Blocking behaviour with Entity Framework Async methods and SQL Server Compact</a> </p>
  
  <p>I am using (via NuGet):</p>
  
  <ul>
  <li>EntityFramework.SqlServerCompact v6.1.3 by Microsoft</li>
  <li>Microsoft.SqlServerCompact v4.0.8876.1 [Is this the ADO.Net provider?]</li>
  </ul>