简体   繁体   English

EF7 beta6:更新时出现ReadAsync错误

[英]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: 我正在使用ASP.NET5和Entity Framework 7.0.0-beta 6创建一个API,当我尝试执行更新时,出现此异常:

An exception of type 'Microsoft.Data.Entity.DbUpdateException' ocurred in mscorlib.dll but was not handled in user code. mscorlib.dll中发生类型'Microsoft.Data.Entity.DbUpdateException'的异常,但未在用户代码中处理。

{"An error occurred while updating the entries. See the inner exception for details."} {“更新条目时发生错误。有关详细信息,请参阅内部异常。”}

Invalid attempt to call ReadAsync when reader is closed. 当阅读器关闭时,无效的尝试调用ReadAsync。

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: 这是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 + info: https : //github.com/aspnet/EntityFramework/issues/2786

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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