控制创建实体框架ObjectContext的频率 [英] Controlling how often an Entity Framework ObjectContext is created

查看:147
本文介绍了控制创建实体框架ObjectContext的频率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个相当大的SQL支持的ASP.NET项目,它使用Entity Framework与数据库进行交互。



我的问题是,我应该创建一个ObjectContext由每个类/方法中的edmx / designer文件定义 - 或者将其围绕一个静态类来实例化,并通过一个实例基本上处理所有请求。



我将有多个用户使用ObjectContext读取和更新数据库,并发和线程安全是我最大的担忧。



编辑:
这段代码最终将在IIS中运行,并且易于回收。

解决方案

在大多数情况下,您只需要单个上下文请求。只有特殊情况可能需要每个请求更多的上下文,但是从不在请求之间共享上下文< a>。


I have a rather large SQL backed ASP.NET project that uses Entity Framework to interact with the database.

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.

I will have multiple users using the ObjectContext to read and update the DB, concurrency and thread safety are my utmost concerns.

EDIT: This code would ultimately run in IIS and be susceptible to recycling.

解决方案

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屋!

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