NHibernate SessionFactory和EF 4.0 ObjectContext之间的区别(Difference between NHibernate SessionFactory and

编程入门 行业动态 更新时间:2024-10-28 13:24:48
NHibernate SessionFactory和EF 4.0 ObjectContext之间的区别(Difference between NHibernate SessionFactory and EF 4.0 ObjectContext)

在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

但是,您不应在ASP.NET应用程序中使用静态ObjectContext,因为静态成员的生命周期超出单个HTTP请求的寿命。 它们实际上与AppDomain的生命周期有关,可能是几分钟或几小时。 实际上,ASP.NET中的静态类成员甚至可以在应用程序的多个线程和用户之间共享。 同时在多个线程内使用相同的ObjectContext实例可能会导致严重的问题。

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

在实体框架的情况下,这被认为是错误的做法吗?

In NHibernate SessionFactory is said to be a heavy object and it is suggested that SessionFactory should be created once in application life span. However once we get handle to SessionFactory, we do call open() on it before doing any DB operation.

In EntityFramework we need to create an object of ObjectContext every time before doing any operation with underlying store. There is no opening as such in case of EF.

My questions is:

Is Creating a context in EF is similar to calling Open() on SessionFactory in NHibernate?

Or should I create ObjectContext once in application life span and share it across?

Update: I found following link on managing ObjectContext lifespan. However in that link author mentions:

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.

But in NHibernate we do the exact same thing. We create SessionFactory as a static field and then use the same instance across application again and again.

Is that what being suggested as a wrong practice in case of Entity Framework?

最满意答案

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

The OC in EF is more like the Session in NH than the SessionFactory.

更多推荐

本文发布于:2023-08-07 01:45:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1458288.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:区别   EF   SessionFactory   NHibernate   Difference

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!