简体   繁体   中英

EF7 beta6: ReadAsync error on Update

I am creating an API using ASP.NET5 and Entity Framework 7.0.0-beta 6, and when I try to execute an update, I get this Exception:

An exception of type 'Microsoft.Data.Entity.DbUpdateException' ocurred in mscorlib.dll but was not handled in user code.

{"An error occurred while updating the entries. See the inner exception for details."}

Invalid attempt to call ReadAsync when reader is closed.

The update operation finish correctly, and the changes are persisted to the database, but I get this exception.

My code is really simple, and I do not try to read anything after the update operation:

public class CompanyRepository : ICompanyRepository
{

    MrBellhopContext _dbcontext;


    public async Task UpdateAsync(Company company)
    {
        _dbcontext.Update(company);
        await _dbcontext.SaveChangesAsync();
    }
}



[Route("api/[controller]")]
public class CompanyController : Controller
{


    [HttpPut]
    public async void UpdateAsync([FromBody] Company company)
    {
        if ((!ModelState.IsValid) || (company == null))
        {
            Context.Response.StatusCode = 400;
            return;
        }
        else
        {
            await _repository.UpdateAsync(company);
        }
    }

}

And this is the Stacktrace:

   at Microsoft.Data.Entity.Update.AffectedCountModificationCommandBatch.<ConsumeAsync>d__6.MoveNext()
--- 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 Microsoft.Data.Entity.Update.ReaderModificationCommandBatch.<ExecuteAsync>d__18.MoveNext()
--- 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 Microsoft.Data.Entity.Update.BatchExecutor.<ExecuteAsync>d__7.MoveNext()
--- 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`1.GetResult()
   at Microsoft.Data.Entity.ChangeTracking.Internal.StateManager.<SaveChangesAsync>d__27.MoveNext()
--- 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`1.GetResult()
   at Microsoft.Data.Entity.DbContext.<SaveChangesAsync>d__27.MoveNext()
--- 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`1.GetResult()
   at MrBellhop.Data.Repositories.Company.CompanyRepository.<UpdateAsync>d__5.MoveNext() in D:\SciOf\MrBellhop\MrBellhop.Data\Repositories\Company\CompanyRepository.cs:line 47
--- 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 MrBellhop.API.Controllers.Company.CompanyController.<UpdateAsync>d__9.MoveNext() in D:\SciOf\MrBellhop\MrBellhop.API\Controllers\Company\CompanyController.cs:line 62

Does anyone know how to solve this issue?

Solved!

I have modified the Controller to return a Task:

[Route("api/[controller]")]
public class CompanyController : Controller
{


    [HttpPut]
    public async Task UpdateAsync([FromBody] Company company)
    {
        if ((!ModelState.IsValid) || (company == null))
        {
            Context.Response.StatusCode = 400;
            return;
        }
        else
        {
            await _repository.UpdateAsync(company);
        }
    }

}

+info: https://github.com/aspnet/EntityFramework/issues/2786

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM