Quantcast
Channel: entityframework Discussions Rss Feed
Viewing all articles
Browse latest Browse all 1793

New Post: Entity Framework 6.1.0 SaveChangesAsync

$
0
0
I figured it out! This is NOT a bug in EF as I mentioned before! It's user! lol :)

Here is the problem that was happening, when, you wait on the Task with the "Wait" method or take the result directly from the "Result" property of the Task, you block the main thread at the same time. When eventually the Task completes inside that method (SaveOrUpdateAsync(TEntity entity)) in the thread pool, it is going to invoke the continuation to post back to the main thread (as it never left it), because SynchronizationContext.Current is available and captured. But here is a problem: the main thread is blocked by "Wait" method and that is how I was getting a deadlock!

To fix deadlock issue I had to specify to not to continue on captured context for context.SaveChangesAsync().
public async Task<int> SaveOrUpdateAsync<TEntity>(TEntity entity)
        where TEntity : class, IContextEntity
    {
        if (entity.Id == 0)
            context.Set<TEntity>().Add(entity);
        else
        {
            TEntity dbEntry = context.Set<TEntity>().Find(entity.Id);
            if (dbEntry != null) dbEntry = entity;
        }

        return await context.SaveChangesAsync().ConfigureAwait(continueOnCapturedContext: false);
    }

Viewing all articles
Browse latest Browse all 1793

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>