Entity Framework 6 / SQL Server CE 4 SaveChangesAsync()

139 views Asked by At

I have the following method running in a non-GUI thread within my application:

private async Task PerformContextSubmitAsync()
{
    try
    {
        await DataContextProvider.GetDefaultContext().SaveChangesAsync();
    }
    catch (Exception ex)
    {
        Log.Error("Error performing context submit", ex);
    }
}

Which is called like this:

await PerformContextSubmitAsync();

The application is a WPF/Prism based application so I'm also publishing events to update a progress bar and file count in the GUI such as:

_eventAggregator.GetEvent<DatabaseProgressEvent>().Publish(new DatabaseProgress(percentDone));

Everything works as expected accept I'm still seeing a "chunking" effect in the UI updates that coincide with the call to SaveChangesAsync().

Basically this is still blocking the thread that this is being executed on. Is this a limitation of SQL Server CE or am I doing something really stupid?

I can find precious little about this searching Google and nothing on SO.

1

There are 1 answers

0
Jammer On BEST ANSWER

After some more testing it appears that I shouldn't be using the await keyword in he call to the async method.

Executing the call like this:

Task.Run(() => PerformContextSubmitAsync());

Doesn't block the original calling thread, but it doesn't seem to actually execute in an asynchronous fashion. It seem to behave more like deferring the execution to a point of time in the future.