更新

时间:2015-08-06 15:31:49

标签: c# asp.net-core entity-framework-core

我正在使用ASP.NET5和Entity Framework 7.0.0-beta 6创建API,当我尝试执行更新时,我得到了这个例外:

  

出现“Microsoft.Data.Entity.DbUpdateException”类型的异常   在mscorlib.dll中但未在用户代码中处理。

     

{“更新条目时发生错误。请参阅内部   细节例外。“}

     

关闭阅读器时无效尝试调用ReadAsync。

更新操作正确完成,更改将持久保存到数据库,但我收到此异常。

我的代码非常简单,在更新操作后我不会尝试读取任何内容:

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);
        }
    }

}

这是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

有谁知道如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

<强>解决!

我修改了控制器以返回任务:

[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