简体   繁体   English

如何在不处理上下文的情况下刷新Entity Framework连接

[英]How can I refresh an Entity Framework connection without disposing of the context

After a few years of using Entity Framework in our applications without issue, our company has starting deploying laptops to our users. 在我们的应用程序中使用Entity Framework数年之久之后,我们公司已开始将便携式计算机部署到我们的用户。 The laptops are being used on-site, with a direct, wired connection to our network. 这些笔记本电脑可在现场使用,并通过有线直接连接到我们的网络。

Since then, we have seen a huge increase of crashes within our programs, with the exception logs showing 从那时起,我们看到程序中崩溃的数量大大增加,异常日志显示

System.Data.SqlClient.SqlException: A transport-level error has occurred when receiving results from the server. 
(provider: Session Provider, error: 19 - Physical connection is not usable)

My theory is that the issue stems from the fact that the laptops, unlike the PCs, go to sleep. 我的理论是,问题出在笔记本电脑不像PC那样进入睡眠状态。 I think that the network adapter is being disabled when the laptop goes to sleep, and re-enabled when it wakes back up. 我认为笔记本电脑进入睡眠状态时会禁用网络适配器,而在笔记本计算机唤醒后会重新启用网络适配器。 I think our programs are still trying to communicate with the server using a connection that is no longer there. 我认为我们的程序仍在尝试使用不再存在的连接与服务器进行通信。

So, my idea was to respond to: 因此,我的想法是回应:

Microsoft.Win32.SystemEvents.PowerModeChanged

I can detect when it wakes up and refresh the connection. 我可以检测到它何时醒来并刷新连接。 The trouble is that the only way that I can seem to do this is to dispose of the current DbContext and instantiate a new one. 麻烦的是,我似乎可以做到这一点的唯一方法是处置当前的DbContext并实例化一个新的DbContext。

The problem with this is that any uncommitted changes are then lost. 问题在于,所有未提交的更改都将丢失。 If the user has been working all day on updating a record, they would lose their work. 如果用户整天都在工作以更新记录,则他们会丢失工作。 Not only that, but we would have to go through all of our applications, and all of our viewmodels and incorporate some sort of kick out of edit mode with notifications to the user. 不仅如此,我们还必须遍历所有应用程序和所有视图模型,并结合某种形式的踢出编辑模式并向用户发送通知。 Not pretty at all. 一点都不漂亮。

A second idea I've had was to create a method to clone a DbContext . 我的第二个想法是创建一个克隆DbContext的方法。 When the computer wakes up, I could create a new DbContext and copy the state from the old one before disposing of it... but some of our data models are massive, and creating a deep clone method for each would be quite the undertaking. 当计算机唤醒时,我可以创建一个新的DbContext并从旧状态复制状态,然后再处理它……但是我们的某些数据模型非常庞大,为每个模型创建深层克隆方法将是一件很艰巨的任务。

It occurs to me that this might still be the way we have to go... but I'd be stupid not to check if anyone knows of a way to refresh the connection of an Entity Framework DbContext without losing it's current state. 在我看来,这可能仍然是我们必须走的路...但是我很愚蠢,不检查是否有人知道一种刷新Entity Framework DbContext连接而又不丢失其当前状态的方法。

I would appreciate any advice that anyone might have. 我会很感激任何人可能提供的任何建议。

I'm not sure, whether it will help in your specific case, but you can check the status of the connection used by your DbConetxt class and eventually reopen it. 我不确定这是否对您的情况有所帮助,但是您可以检查DbConetxt类使用的连接状态,并最终重新打开它。

if(context.Database.Connection.State == ConnectionState.Closed) {
    context.Database.Connection.Open();
}

it it also possible to pass a connection to the DbContext in it's constructor and manage this connection manually. 还可以将连接传递到其构造函数中的DbContext并手动管理此连接。

var conn = new SqlConnection("{connectionString}"));
var context =  new DbContext(conn, contextOwnsConnection: false);

...

if(conn.State == ConnectionState.Closed) {
    conn.Open(); 
}
context.SaveChanges(); 

...

context.Dispose();
conn.Dispose();

There are some limitations, if you want to use this code in the EF 5 or earlier versios. 如果要在EF 5或更早的版本中使用此代码,则存在一些限制。 See official documentation. 请参阅官方文档。

I was just going to comment but I don't have enough reputation points to do that, so anyway I was wondering if it is not possible to work in a disconnected state with Entity Framework? 我只是要发表评论,但我没有足够的声誉点来做到这一点,所以无论如何,我想知道是否无法使用Entity Framework在断开状态下工作? Maybe the article in the link below can help: 也许下面链接中的文章会有所帮助:

https://msdn.microsoft.com/en-us/data/jj592676.aspx https://msdn.microsoft.com/zh-CN/data/jj592676.aspx

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

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