ADO.NET 实体框架 ObjectContext - 缓存问题

发布于 2024-08-21 01:56:07 字数 180 浏览 7 评论 0原文

我想知道将实体框架的 ObjectContext 对象缓存在缓存中是否明智;这会给我带来多个连接的问题,同时用户也会遇到问题吗?

我收到了类似以下错误:“连接当前已关闭”,想知道这是否是由于多个用户和缓存 ObjectContext 造成的,或者是否与多次点击刷新或停止页面并快速执行其他操作有关(我们确实这样做是为了得到错误)。

I was wondering if it was wise to cache the Entity Framework's ObjectContext object in the Cache; will this give me issues with multiple connections at the same time that the user will experience issues with that?

I've gotten errors like: 'connection is currently closed' and wondered if that was due to multiple users and caching the ObjectContext, or if it was related to say hitting refresh multiple times or stopping the page and quickly doing something else (something that we did do to get the error).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

雾里花 2024-08-28 01:56:07

我同意上面的观点。但是,我确实将对象上下文缓存在 HttpContext.Current.Items 集合中,没有任何问题。也很好读:

http://dotnetslackers.com/articles/ado_net/managing-entity-framework-objectcontext-lifespan-and-scope-in​​-n-layered-asp-net-applications.aspx

I agree with above. However, I do cache the object context in HttpContext.Current.Items collection without any issues. Also a good read:

http://dotnetslackers.com/articles/ado_net/managing-entity-framework-objectcontext-lifespan-and-scope-in-n-layered-asp-net-applications.aspx

没︽人懂的悲伤 2024-08-28 01:56:07

我不建议这么做。 ObjectContext 需要处于活动状态才能观察您正在使用的任何实体的更改,否则您需要在缓存 ObjectContext 之前断开任何活动实体的连接。

如果没有活动实体,则不需要缓存 ObjectContext。在 EFv1 中,使用断开连接的实体充其量是有问题的,因此我要么不缓存,要么等待实体框架 v4,它允许更多可管理的实体(自跟踪实体、POCO 实体等)。

只是想我会添加最后一点 - 多线程 - 也可能有问题。应用更改将尝试提交 ObjectContext 跟踪的所有更改。如果多个用户共享一个上下文......那么,希望你能看到问题......

I wouldn't advise it. The ObjectContext needs to be active to observe changes to any entities you are actively working with or you'd need to disconnect any active entities prior to caching the ObjectContext.

If you have no active entities, then there's no real need to cache an ObjectContext. In EFv1 working with disconnected entities was problematic at best, so I'd either not cache or wait for the Entity Framework v4 which allows for more manageable entities (self tracking entities, POCO entities etc).

Just thought I'd add one last point - multiple threads - could be problematic as well. Applying Changes will attempt to commit all changes tracked by the ObjectContext. If multiple users are sharing a single Context... well, hopefully you can see the problems..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文