Pradeep
Pradeep

Reputation: 3276

Difference between NHibernate SessionFactory and EF 4.0 ObjectContext

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?

Upvotes: 3

Views: 1600

Answers (1)

Craig Stuntz
Craig Stuntz

Reputation: 126567

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

Upvotes: 3

Related Questions