C#中的多线程服务器与实体框架合作 [英] c# working with Entity Framework in a multi threaded server

查看:420
本文介绍了C#中的多线程服务器与实体框架合作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是在多线程服务器与实体框架工作的最佳实践?
我使用实体框架的ObjectContext 来管理我所有的数据库操作,现在我知道这种情况下不是线程安全的,所以现在当我需要用它来执行一些DB动作我围绕着它与锁定语句是安全的。这是我应该怎么办呢?

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??

推荐答案

一些快速意见的实体框架在多线程环境中:

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


  • 请不要使用带有锁定一个唯一的上下文(无单例模式)

  • 服务(您需要实例化和处置的每个请求一个上下文

  • 缩短使用寿命范围内尽可能多地

  • 请实施并发控制系统。乐观并发可以与实体框架(<一个可以轻松实现href=\"http://blogs.msdn.com/b/alexj/archive/2009/05/20/tip-19-how-to-use-optimistic-concurrency-in-the-entity-framework.aspx\">how-to).这将确保当您使用的是没有及时更新
  • 实体不要覆盖在DB的变化
  • 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?

您的 的可以只使用一个背景,但我们强烈劝阻的除非你真的知道你在做什么

You could use only one context, but it's strongly discouraged unless you really know what you are doing.

我看到的,往往这样的做法会发生两个主要问题:

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#中的多线程服务器与实体框架合作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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