【问题标题】:cannot access a disposed object while updating the database using EF Core and Azure Functions使用 EF Core 和 Azure Functions 更新数据库时无法访问已释放的对象
【发布时间】:2020-01-13 18:19:09
【问题描述】:

我正在尝试使用 EF Core 使用以下方法从数据库中获取单行,

 IncidentRequestDetailsModel dbObject = _context.IncidentRequestDetails.SingleOrDefault(no => no.Ticket_No == requestList[i].Ticket_No);

也试过了

IncidentRequestDetailsModel dbObject = _context.IncidentRequestDetails.Where(no => no.Ticket_No == requestList[i].Ticket_No).FirstOrDefault();

还有这个

IncidentRequestDetailsModel dbObject = await _context.IncidentRequestDetails.FindAsync(requestList[i].Id);

它正在抛出这个错误

如果你在上下文中调用 Dispose(),或者包装上下文 在 using 语句中。如果你使用依赖注入,你 应该让依赖注入容器负责处理 上下文实例。对象名称:'uploadContext'。'

我的最终目标是使用以下整体代码更新数据库,

 incidentRequestDetailsModelDB.Status = incidentRequestDetailsModel.Status;
 _context.IncidentRequestDetails.Update(incidentRequestDetailsModelDB);
 await _context.SaveChangesAsync();

我将 EF Core 与 Azure Functions 和 .Net Core 2.1 结合使用。

【问题讨论】:

标签: c# entity-framework azure-functions dbcontext .net-core-2.1


【解决方案1】:

这与查询的语法没有任何关系。这意味着您的代码的某些部分在您尝试查询的代码部分之前调用了_context 上的Dispose() 方法。

您需要追踪您调用Dispose() 的位置,或退出 using 语句。并确保您没有在 Azure 函数的多次调用中重复使用相同的上下文实例。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-06
    • 1970-01-01
    • 2021-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-16
    相关资源
    最近更新 更多