服务器停机导致EF从无效操作异常中恢复 [英] EF recovery from invalidoperationexception caused by server being down

查看:57
本文介绍了服务器停机导致EF从无效操作异常中恢复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个依赖于EF的Windows服务,在服务器关闭之前,它运行良好.问题是服务器再次启动后,它没有自行修复,仍然抛出错误:

I had a windows service relying on EF, and it was running fine until the server went down. The problem was after the server went up again, it didn't fix itself and still threw the error:

内部错误:执行该命令需要打开且可用的连接.连接的当前状态已断开.

INTERNAL ERROR: Execution of the command requires an open and available connection. The connection's current state is broken.

不是很精通EF我不知道如何从这种情况中恢复,以及为什么首先发生这种情况?有人曾经发生过这种事吗?

Not being very proficient in EF I'm not aware of how I could recover from such a scenario, and why it occurred in the first place? Anyone had this happen before?

推荐答案

ObjectContext的生存期是多少?如果在服务的生命周期内使用单个ObjectContext,则可能会导致类似这样的问题.您应该将ObjectContext的生存期范围缩小到较小的时间范围,为每个工作单元创建一个新的时间范围.

What is the lifetime of your ObjectContext? If you are using a single ObjectContext for the lifetime of your service that would cause problems like this. You should instead scope the ObjectContext lifetime down to a smaller window of time, creating a new one for each unit of work.

这篇关于服务器停机导致EF从无效操作异常中恢复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆