控制多久实体框架ObjectContext的创建 [英] Controlling how often an Entity Framework ObjectContext is created
问题描述
我有一个使用实体框架与数据库交互的一个相当大的SQL支持ASP.NET项目。
I have a rather large SQL backed ASP.NET project that uses Entity Framework to interact with the database.
我的问题是,我应该创建ObjectContext中的每一等级的EDMX /设计文件中定义一个实例/方法 - 或者将其套在一个静态类,将实例化的onload,基本上通过一个实例来处理所有的请求。
My question is, should I create an instance of the ObjectContext defined by the edmx/designer files in each class/method -or- wrap it around a static class that would instantiate onload and basically handle all requests through one instance.
我将有多个用户使用的ObjectContext读取和更新数据库,并发性和线程安全是我最大的担忧。
I will have multiple users using the ObjectContext to read and update the DB, concurrency and thread safety are my utmost concerns.
编辑: 这code将在IIS最终运行,并容易受到利用。
This code would ultimately run in IIS and be susceptible to recycling.
推荐答案
在大多数情况下,你只需要为每一个请求一个方面。只有特殊情况下可以根据需要每一个请求,但<更上下文href="http://stackoverflow.com/questions/3653009/entity-framework-and-connection-pooling/3653392#3653392">never其中要求份额方面。
In most cases you just need single context for every request. Only special scenarios can need more contexts per single request but never share context among requests.
这篇关于控制多久实体框架ObjectContext的创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!