EF 6 – Async executing SQL command in SaveChangesAsync method throws NotSupportedException

.net async-await c# entity-framework entity-framework-6

Question

I have removed theSaveChangesAsync approach to myDbContext to invoke a number of stored procedures. I first make theSaveChangesAsync of DbContext I then run a stored procedure for every updated entity.

Every async method call is anticipated.

The exception raised by EF is as follows:

System.NotSupportedException: A second operation started on this context before a previous asynchronous operation completed. Use 'await' to ensure that any asynchronous operations have completed before calling another method on this context. Any instance members are not guaranteed to be thread safe.
   at System.Data.Entity.Internal.ThrowingMonitor.EnsureNotEntered()
   at System.Data.Entity.Core.Objects.ObjectContext.ExecuteStoreCommandAsync(TransactionalBehavior transactionalBehavior, String commandText, CancellationToken cancellationToken, Object[] parameters)
   at System.Data.Entity.Internal.InternalContext.ExecuteSqlCommandAsync(TransactionalBehavior transactionalBehavior, String sql, CancellationToken cancellationToken, Object[] parameters)
   at System.Data.Entity.Database.ExecuteSqlCommandAsync(TransactionalBehavior transactionalBehavior, String sql, CancellationToken cancellationToken, Object[] parameters)
   at System.Data.Entity.Database.ExecuteSqlCommandAsync(String sql, CancellationToken cancellationToken, Object[] parameters)
   at System.Data.Entity.Database.ExecuteSqlCommandAsync(String sql, Object[] parameters)
   at Common.Dal.AuditDbContext.<>c__DisplayClass20_0.<WriteAuditsParallelAsync>b__0(Audit x) in 
   at System.Linq.Enumerable.WhereSelectListIterator`2.MoveNext()
   at System.Threading.Tasks.Task.WhenAll[TResult](IEnumerable`1 tasks)
   at Common.Dal.AuditDbContext.<WriteAuditsParallelAsync>d__20.MoveNext() in
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
   at Common.Dal.AuditDbContext.<SaveToDatabaseAsync>d__18.MoveNext() in

Here's my code:

public override Task<Int32> SaveChangesAsync( CancellationToken cancellationToken )
{
    var modified = this.GetModifiedOrDeletedEntities();
    var added = this.GetAddedEntities();

    var audits = AuditService.GetAudits( GetObjectStateManager(), modified );

    //Call SaveChangesAsync
    var result = await base.SaveChangesAsync( cancellationToken );

    audits.AddRange( AuditService.GetAudits( GetObjectStateManager(), added ) );

    //Call stored prcedures
    await WriteAuditsAsync( audits, user );

    return result;
}

private async Task WriteAuditsAsync(List<Audit> audits, String user)
{
    foreach ( var audit in audits)
    {
        try
        {
            ...
            //Execute SQL command 
            await Database.ExecuteSqlCommandAsync(myCommand, myParameters);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex);
        }
    }
}

Why is this exception being thrown by EF?

1
3
11/6/2015 9:56:41 AM

Accepted Answer

ZZZ_tmp
3
11/6/2015 9:50:43 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow