- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我编写了几个与 Entity Framework 一起使用的应用程序。我曾经使用 using 子句。通过这个子句,我确信数据库连接已正确关闭。
但是,在某些情况下,我的数据库上下文对象是类的字段。它的实例化在声明中,并且没有using子句。
我刚刚发现,在这种情况下,Dispose() 方法没有被调用。使用 using 子句,会自动调用 Dispose()
所以我的问题是:如果没有在数据库上下文中调用 Dispose() 会发生什么?析构函数被调用,那么您认为析构函数会关闭数据库吗?或者我应该在容器对象的析构函数上手动调用 Dispose() ,如下所示:
class MyClass
{
public MyDbContext ctx = new MyDbContext();
....
~MyClass()
{
ctx.Dispose();
}
}
谢谢
最佳答案
您绝对不能调用 Dispose
。特别明确
。
当您使用using
时,它是try-finally
block 中的隐式Dispose()
。这很好。但是您不需要明确地
这样做。
为什么?
发生异常
时,单独
或显式
Dispose
语句可能会被错过
发生更早
。因此无法保证显式
Dispose
。
这是来自Rowan Miller [MSFT]:
The default behavior of DbContext is that the underlying connection is automatically opened any time is needed and closed when it is no longer needed. E.g. when you execute a query and iterate over query results using “foreach”, the call to IEnumerable.GetEnumerator() will cause the connection to be opened, and when later there are no more results available, “foreach” will take care of calling Dispose on the enumerator, which will close the connection. In a similar way, a call to DbContext.SaveChanges() will open the connection before sending changes to the database and will close it before returning.
Given this default behavior, in many real-world cases it is harmless to leave the context without disposing it and just rely on garbage collection.
That said, there are two main reason our sample code tends to always use “using” or dispose the context in some other way:
The default automatic open/close behavior is relatively easy to override: you can assume control of when the connection is opened and closed by manually opening the connection. Once you start doing this in some part of your code, then forgetting to dipose the context becomes harmful, because you might be leaking open connections.
DbContext implements IDiposable following the recommended pattern, which includes exposing a virtual protected Dispose method that derived types can override if for example the need to aggregate other unmanaged resources into the lifetime of the context.
您可以在这里阅读更多相关信息:Do I always have to call Dispose() on my DbContext objects? Nope
关于entity-framework - EntityFramework 上下文处置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39526038/
全部: 我们使用 ASP.NET Web 窗体开始了我们的项目。 我们的 ASP.NET C# web 应用程序在以下环境中使用 .NET Framework 4 ASP.NET Web Forms.
我在尝试为 Entity Framework Core 添加迁移到 Code First 项目时遇到错误,这里是详细信息... 我创建了一个新的 ASP.Net Core Web 项目(VS 2017
Error 1 Copying file bin\EntityFramework.SqlServer.xml to obj\Debug\Package\PackageTmp\bin\Entit
我有一个WPF应用程序,我使用了MVVM体系结构,数据库sql server compact 4。 首先是Entityframework.sqlserverCompact数据库,我使用了通用存储库模式
例如,将 products.Where(p => p.LastModifiedOn > someDate) 总是以相同的订单返回产品(无论如何订购,只要每次调用相同的代码时都是一致的)?或者我应该添加
好吧,假设我有一个用户表,然后是一个记录所有登录尝试的表(Id(计数器)、userId、时间以及是否成功) 现在在我的实体模型中,我希望它成为 2 个关联的一个,包含所有成功尝试的时间和一个包含所有不
我正在努力处理如何最好地跨多个 Visual Studio 项目分解域模型。我正在使用 EntityFramework 4 和企业类型模式来合理化一个可以重用于各种应用程序的系统。最终我想得到一组可以
是否可以使用 POCO 和 EF 处理以下情况?我想要一个 Vehicles 表,有一些 bool 字段,如 IsFast、IsVan、IsTruck,然后有一个 Vehicles 类,有一个 Fas
当我通过传递比较泛型类型 TId 的谓词调用 DbSet.FirstOrDefault() 时,出现以下异常: unable to create a constant value of type 's
我正在使用 EF5 将一些数据从一个数据库迁移到另一个数据库。我通常会使用 SQL 来做这样的事情,但是我需要其他功能(比如在 MembershipProvider 中创建用户)并且希望在 EF 中完
我创建了一个单独的项目并将我的模型复制到这个项目中。我引用了 using System.ComponentModel.DataAnnotations 我的模型定义如下: public class F
上下文: 我有两个类在 SQL 中有两个对应的表。 系统信息 EF 版本: SQL 版本:2012 类 public class Employee { public Employee(
我有一个定义了主从关系的 Entity Framework 对象。详细信息对象集合有一个导航属性。 在稍后的代码中,我尝试使用 AutoMapper 将其中一个主对象映射到数据传输对象。但是,数据传输
有没有办法让这个通用 Context.SalesEntity.Where(t=>t.id==3).Delete(); 有点像 private void DoWork(Expression> predi
来自 NHibernate 背景 - 我有一些审计列来反射(reflect)谁最后编辑了一条记录,以及该编辑发生的时间。 我已经覆盖了 SaveChanges 以寻找一个公共(public)基类,并使
我是 EntityFramework 6 的新手,一直在 POCO 中遇到问题。 以下是我的代码 Domain.cs public abstract class BaseEntity { [K
我刚在单元测试项目中遇到一个问题,坚持认为它需要 EntityFramework 的引用,我确信它不需要它。其他项目正在引用单元测试项目正在引用/测试的项目/扩展方法,并且在不引用 EntityFra
下面的代码应该做什么?在此代码中添加包含表的目的是什么,它应该对与 PersonId 相关的 JobType 进行级联删除,还是只删除具有指定 PersonId 的 Person ? db.tblPe
我试图了解延迟加载在 Entity Framework 6 中是如何工作的。在我看来,延迟加载只适用于 DbContext,其中“to-be-loaded”属性已经存在访问过。请查看下面的示例 - 测
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: Entity Framework Validation EntityFramework有什么好的灵活的验证框
我是一名优秀的程序员,十分优秀!