EF(实体框架)使用“使用”声明 [英] EF (entity framework) usage of "using" statement
问题描述
我有一个关于MVC的项目。我们选择EF作为我们的数据库交易。我们为BLL层创建了一些管理者。我发现了很多例子,其中使用了<使用
语句,即
public Item GetItem(long itemId)
{
using(var db = new MyEntities())
{
return db.Items.Where(it => it .ItemId == itemId&&!it.IsDeleted).FirstOrDefault();
$ b $ p
$ b 这里我们创建一个新的DBcontext实例 MyEntities()
。
我们使用使用
以确保正确使用IDisposable对象。
这只是我经理中的一种方法。但我有超过十个。
每次我从经理调用任何方法时,我都会使用使用
statemant和
在内存中创建另一个DBcontext。垃圾收集器(GC)会处理它们吗?谁知道?
但是还有另一种使用经理方法的方法。
我们创建一个全局变量:
private readonly MyEntities db = new MyEntities();
并在每个方法中使用DBcontext而不使用使用
声明。并且方法如下所示:
public Item GetItem(long itemId)
{
return db.Items .Where(it => it.ItemId == itemId&!it.IsDeleted).FirstOrDefault();
问题:
- 使用DBcontext变量的正确方法是什么?
- 如果我们不使用
usage
>声明(因为它影响性能) - GC将为此做所有事情?
我是EF中的一名新秀使用和仍然没有找到这个问题的唯一答案。
解决方案我想你会发现许多暗示这种风格的模式。不只是我或亨克
DBContext处理
- 是,理想情况下使用DBContext子类型的语句
- 更好的工作单元模式,使用Using进行管理,上下文并处理上下文这是一个来自Tom Dykstra的UoW示例中的一个
- 工作管理器应该是新的每个Http请求
- 上下文不是线程安全的,因此确保每个线程都有自己的上下文。
- 在幕后。
- 测试上下文创建时间。经过几次Http请求。您仍然有疑虑吗?
- 如果您将上下文存储为静态,则会出现问题。任何类型的并发访问都会受到影响,如果您使用并行AJAX调用,则假设使用静态单个上下文时出现90%以上的问题。
I have a project on MVC. We chose EF for our DB transactions. We created some managers for the BLL layer. I found a lot of examples, where "using
" statement is used, i.e.
public Item GetItem(long itemId)
{
using (var db = new MyEntities())
{
return db.Items.Where(it => it.ItemId == itemId && !it.IsDeleted).FirstOrDefault();
}
}
Here we create a new instance of DBcontext MyEntities()
.
We using "using
" in order to "ensure the correct use of IDisposable objects."
It's only one method in my manager. But I have more than ten of them.
Every time I call any method from the manager I'll be using "using
" statemant and
create another DBcontext in the memory. When garbage collector (GC) will dispose them? Who knows?
But there is another alternative of usage of the manager methods.
We create a global variable:
private readonly MyEntities db = new MyEntities();
and use DBcontext in every method without "using
" statement. And method looks like this:
public Item GetItem(long itemId)
{
return db.Items.Where(it => it.ItemId == itemId && !it.IsDeleted).FirstOrDefault();
}
Questions:
- What is the proper way of using DBcontext variable?
- What if we wouldn't use "
usage
" statement (because it affects the performance) - GC will do all for that?
I'm a "rookie" in EF usage and still haven't found the univocal answer for this question.
解决方案 I think you will find many suggesting this style of pattern. Not just me or Henk
DBContext handling
- Yes, Ideally Using statements for DBContext subtypes
- Even better Unit Of Work patterns that are managed with Using, that have a context and dispose the context Just 1 of many UoW examples, this one from Tom Dykstra
- The Unit Of Work Manager should be New each Http request
- The context is NOT thread safe so make sure each thread has its own context.
- Let EF cache things behind the scenes.
- Test Context creation times. after several Http request. Do you still have a concern?
- Expect problems if you store the context in static. any sort of concurrent access will hurt and if you are using parallel AJAX calls, assume 90+% chance of problems if using a static single context.
For some performance tips, well worth a read
这篇关于EF(实体框架)使用“使用”声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!