它是安全使用每线程一个实体框架上下文? ......是吗?怎么样? [英] Is it safe to use one Entity Framework Context per thread? ... yes? how?

查看:143
本文介绍了它是安全使用每线程一个实体框架上下文? ......是吗?怎么样?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许这些都是在一两个问题,我使用的每个请求对应一个EF上下文,但我想用每个线程的一个,因为我要提出请求时在另一个线程一些复杂的任务。

Probably these are two questions in one, I am using one EF context per request, but I want to use one per thread, because I am going to make some complex task in another thread during the request.

那么,是不是安全的?如果答案是肯定的,怎么办呢?如何存储对象的线程,并让他们回来?

So, is it safe? If the answer is yes, how to do it? how to store objects in thread and get them back?

感谢您提前。

推荐答案

它是安全只有当你有超过线程的完全控制。这意味着你必须创建线程,线程完成作业后,必须死。如果您使用线程池来查询工作,你不能使用每个线程上下文。线程池例如用于服务ASP.NET,ASP.NET MVC或WCF请求的线程。上下文不是线程安全的,所以不要线程之间共享。

It is safe only if you have full control over the thread. It means you must create the thread and thread must die after finishing the job. If you use thread pool to query work you mustn't use context per thread. Thread pool is used for example for threads serving ASP.NET, ASP.NET MVC or WCF requests. Context is not thread safe so do not share it among threads.

要存储每个线程使用的东西静态变量和 ThreadStaticAttribute 标记它。

To store something per thread use static variable and mark it with ThreadStaticAttribute.

这篇关于它是安全使用每线程一个实体框架上下文? ......是吗?怎么样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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