MVC,和的DbContext多线程 [英] MVC, DbContext and Multithreading

查看:1211
本文介绍了MVC,和的DbContext多线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

另外有许多有关这些主题的问题,每个人都有自己的见解。
也许有人可以给我一个很好的答案就以下问题。

There are lots of questions about these subjects separately and everyone have their own opinion. Maybe someone can give me a good answer regarding the following issue.

我有一个使用的EntityFramework访问数据库的Asp.NET MVC Web服务。
有一个单控制器和它的一个实例创建的每个用户发出的Web服务请求的时间。
每一个要求快。它只是得到了一些数据从数据库,改变它,然后将其保存。

I have an Asp.NET MVC web service which uses EntityFramework for accessing the DB. There's a single controller and an instance of it is created each time a user makes a request to the web service. Every request is fast. It just gets some data from DB, changes it and then saves it.

问题当然是如何保持的DbContext(因为它不是线程安全)
在控制器的构造函数我创建的DbContext的一个实例。
在控制器的Dispose()我的处置的DbContext。

The question of course is how to maintain the DbContext (since it's not thread safe)? On the ctor of the controller i create an instance of DbContext. On the Dispose() of the controller I Dispose the DbContext.

我已经看到了一些帖子,这不是一个很好的做法,创造每每个请求的实例。
难道不是吗?

I've seen on some posts that it's not a good practice to create an instance per every request. Isn't it?

谢谢,
艾迪。

Thanks, Edi.

推荐答案

在的DbContext被设计为与每个请求被实例化。它实现了IDisposable和实例是一个低成本的运作。连接池的数据库是内部处理。

The DbContext is designed to be instantiated with each request. It implements IDisposable and instantiating is a low-cost operation. Connection pooling to the database is handled internally.

更多信息:

<一个href=\"http://stackoverflow.com/questions/3653009/entity-framework-and-connection-pooling/3653082#3653082\">Entity框架和连接池

这篇关于MVC,和的DbContext多线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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