简体   繁体   English

EF(实体框架)使用“使用”语句

[英]EF (entity framework) usage of “using” statement

I have a project on MVC. 我有一个关于MVC的项目。 We chose EF for our DB transactions. 我们为数据库交易选择了EF。 We created some managers for the BLL layer. 我们为BLL层创建了一些管理器。 I found a lot of examples, where " using " statement is used, ie 我找到了很多例子,其中usingusing ”语句,即

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() . 这里我们创建一个DBcontext MyEntities()的新实例。 We using " using " in order to "ensure the correct use of IDisposable objects." 我们使用“ using ”以“确保正确使用IDisposable对象”。

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 " statement and create another DBcontext in the memory. 每次我从管理器调用任何方法时,我将使用“ using ”语句并在内存中创建另一个DBcontext。 When will the garbage collector (GC) dispose them? 垃圾收集器(GC)什么时候处理它们? Does anyone know? 有人知道吗?

But there is alternative 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. 并且在没有“ using ”语句的每个方法中使用DBcontext。 And method looks like this: 方法看起来像这样:

public Item GetItem(long itemId)
{
    return db.Items.Where(it => it.ItemId == itemId && !it.IsDeleted).FirstOrDefault();
}

Questions: 问题:

  1. What is the proper way of using DBcontext variable? 使用DBcontext变量的正确方法是什么?
  2. What if we wouldn't use " usage " statement (because it affects the performance) - GC will do all for that? 如果我们不使用“ usage ”声明(因为它会影响性能)怎么办 - GC会为此做些什么?

I'm a "rookie" in EF usage and still haven't found the unequivocal answer for this question. 我是EF使用中的“新手”,但仍然没有找到这个问题的明确答案。

I think you will find many suggesting this style of pattern. 我想你会发现许多暗示这种风格的模式。 Not just me or Henk DBContext handling 不只是我或Henk DBContext处理

  • Yes, Ideally Using statements for DBContext subtypes 是的,理想情况下使用DBContext子类型的语句
  • 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 使用Using管理的更好的工作单元模式,具有上下文并处理上下文只有很多UoW示例中的一个,这个来自Tom Dykstra
  • The Unit Of Work Manager should be New each Http request 每个Http请求都应该是新的工作单元管理器
  • The context is NOT thread safe so make sure each thread has its own context. 上下文不是线程安全的,因此请确保每个线程都有自己的上下文。
  • Let EF cache things behind the scenes. 让EF在幕后缓存东西。
  • Test Context creation times. 测试上下文创建时间。 after several Http request. 经过几次Http请求。 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. 任何类型的并发访问都会受到影响,如果您使用并行AJAX调用,假设使用静态单个上下文时会出现90 +%的问题。

For some performance tips, well worth a read 对于一些性能提示,非常值得一读

The proper or best practice way of using DBContext variable is with the Using. 使用DBContext变量的正确或最佳实践方法是使用。

    using (var db = new MyEntities())
    {
        return db.Items.Where(it => it.ItemId == itemId && !it.IsDeleted).FirstOrDefault();
    }

The benefit is many things are done automatically for us. 好处是许多事情都是我们自动完成的。 For example once the block of code is completed the dispose is called. 例如,一旦完成代码块,就会调用dispose。

Per MSDN EF Working with DbContext 每个MSDN EF使用DbContext

The lifetime of the context begins when the instance is created and ends when the instance is either disposed or garbage-collected. 上下文的生命周期在创建实例时开始,在实例处理或垃圾收集时结束。 Use using if you want all the resources that the context controls to be disposed at the end of the block. 如果您希望将上下文控制的所有资源放置在块的末尾,请使用。 When you use using, the compiler automatically creates a try/finally block and calls dispose in the finally block. 使用时,编译器会自动创建一个try / finally块,并在finally块中调用dispose。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM