.net - NHibernate SessionFactory 和 EF 4.0 ObjectContext 之间的区别

标签 .net nhibernate entity-framework orm entity-framework-4

在NHibernate中SessionFactory被称为重对象,建议在应用程序生命周期内创建一次SessionFactory。然而,一旦我们获得 SessionFactory 的句柄,我们就会在执行任何数据库操作之前对其调用 open()。

在 EntityFramework 中,每次对底层存储进行任何操作之前,我们都需要创建一个 ObjectContext 对象。在 EF 的情况下没有这样的开口。

我的问题是:

在 EF 中创建上下文类似于在 NHibernate 中对 SessionFactory 调用 Open() 吗?

或者我是否应该在应用程序生命周期内创建一次 ObjectContext 并共享它?

更新: 我找到了以下有关管理 ObjectContext 生命周期的链接。但是在该链接中作者提到:

http://dotnetslackers.com/articles/ado_net/Managing-Entity-Framework-ObjectContext-lifespan-and-scope-in-n-layered-ASP-NET-applications.aspx#1390

However, you shouldn’t use a static ObjectContext in an ASP.NET application, since static members have a lifespan beyond that of a single HTTP request. They’re actually bound to the lifespan of the AppDomain, which might be minutes or hours. In fact, static class members in ASP.NET are even shared between multiple threads and users of the application. Using the same ObjectContext instance from within multiple threads simultaneously can cause serious problems.

但在 NHibernate 中我们做完全相同的事情。我们将 SessionFactory 创建为静态字段,然后一次又一次地在应用程序中使用相同的实例。

在 Entity Framework 的情况下,这是否被认为是错误的做法?

最佳答案

EF中的OC更像是NH中的Session而不是SessionFactory。

关于.net - NHibernate SessionFactory 和 EF 4.0 ObjectContext 之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3799160/

相关文章:

c# - 无法加载文件或程序集 'Newtonsoft.Json,版本 = 10.0.0.0

.net - Dispose 方法的线程安全性?

nhibernate - 使用 Fluent NHibernate、Oracle 10g 和 OracleClientConfiguration.Oracle10 映射 clob

c# - Entity Framework 中的任何 Vs 计数

c# - Uninstall_Init/Uninstall_Exit 我如何在 C# 中访问它们

c# - 连接字符串加密,什么意思?

sqlite - NHibernate - 一对多只是不使用 SQLite

string - 如何将枚举映射为数据库中的字符串

c# - 防止 EF 5 生成属性

c# - 删除和查询,没有 DbContext.SaveChanges