c#在多线程服务器中使用Entity Framework [英] c# working with Entity Framework in a multi threaded server

查看:522
本文介绍了c#在多线程服务器中使用Entity Framework的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在多线程服务器中使用实体框架的最佳做法是什么?
我使用实体框架 ObjectContext 来管理我的所有数据库操作,现在我知道这个上下文不是线程安全的,所以现在当我需要使用它执行一些数据库操作我用 lock 语句环绕它是安全的。这是我应该怎么做呢?

What is the best practice for working with entity framework in a multi threaded server? I'm using entity framework ObjectContext to manage all my database actions, now I know this context isn't thread safe, so for now when I need to use it to perform some db actions I surround it with lock statement to be safe. Is this how I should do it??

推荐答案

在多线程环境中对Entity Framework的一些快速建议:

Some quick advices for Entity Framework in a multi-threaded environment:


  • 不要使用锁定(无单例模式)的独特上下文

  • 提供无状态服务(您需要实例化并处理一个上下文每个请求
  • 尽可能缩短上下文生命周期

  • href =http://en.wikipedia.org/wiki/Concurrency_control =noreferrer>并发控制系统。乐观并发可以使用Entity Framework轻松实现( how-to )。这将确保您在使用未更新的实体时不会覆盖数据库中的更改

  • Don't use a unique context with locks (no singleton pattern)
  • Provide stateless services (you need to instantiate and dispose one context per request)
  • Shorten the context lifetime as much as possible
  • Do implement a concurrency-control system. Optimistic concurrency can be easily implemented with Entity Framework (how-to). This will ensure you don't overwrite changes in the DB when you use an entity that is not up-to-date

我有点困惑,我认为使用一个上下文是好的,因为
它做一些捕捉我相信,所以当我处理相同的
实体在连续的请求中使用相同的
上下文,然后每次创建一个新的上下文更快。那么为什么对
使用它是好的,如果它更慢,仍然不线程安全吗?

I'm a bit confused, I thought that using one context is good because it does some catching I believe, so when I'm dealing with the same entity in consecutive requests it be much faster to use the same context then creating a new context every time. So why does it good to use it like this if It slower and still not thread safe?

只能使用一个上下文,但除非您真正知道自己正在做什么,否则非常不鼓励

我遇到两个主要问题通常会发生这样的方法:

I see two main problems that often happen with such an approach:


  1. 你会使用大量的内存,因为你的上下文将永远不会被处置,所有操作的实体将被缓存在内存中(每个显示在查询结果中的实体都被缓存)。

  1. you'll use a lot of memory as your context will never be disposed and all manipulated entities will be cached in memory (each entity that appears in the result of a query is cached).

从另一个程序/上下文修改数据。例如,如果您直接在数据库中修改某些内容,并且关联的实体已经缓存在您的唯一上下文对象中,那么您的上下文将永远不会知道直接在数据库中进行的修改。您将使用不是最新的缓存实体,并相信我,这将导致难以找到和修复的问题。

you'll face lots of concurrency issues if you modify you data from another program/context. For example, if you modify something directly in your database and the associated entity was already cached in your unique context object, then your context won't ever know about the modification that was made directly in the database. You'll work with a cached entity that is not up-to-date, and trust me, it'll lead to hard-to-find-and-fix issues.

也不必担心使用多个上下文的性能:每个请求创建/处理新上下文的开销几乎无关紧要,90%的用例。记住,创建新的上下文不一定创建到数据库的新连接(因为数据库通常使用连接池)。

Also don't worry about performances of using multiple contexts: the overhead of creating/disposing a new context per request is almost insignificant in 90% of use cases. Remember that creating a new context does not necessarily create a new connection to the database (as the database generally uses a connection pool).

这篇关于c#在多线程服务器中使用Entity Framework的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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