Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between DbContext and ObjectContext

From MSDN:

Represents a combination of the Unit-Of-Work and Repository patterns and enables you to query a database and group together changes that will then be written back to the store as a unit. DbContext is conceptually similar to ObjectContext.

I though DbContext only handle the connection to the DB and the number of threads working against the DB.

Now I understand it contains the tracking mechanism? I thought this was in the ObjectContext.

So what is (in plain English) the difference between them?

like image 272
Elad Benda Avatar asked Feb 13 '13 16:02

Elad Benda


2 Answers

DbContext is a lightweight version of the ObjectContext class, which is laid almost right on top of ObjectContext (there is even a way to get to the ObjectContext from just the DbContext). It's also a lot easier to use, IMO, and makes CRUD operations a sinch.

For better information, who better to look to than Julie Lerman for more info on the differences, as was brought into EF 4.1.

like image 123
Corey Adler Avatar answered Oct 17 '22 14:10

Corey Adler


the DbContext is a smaller API exposing the most commonly used features of the ObjectContext. In some cases, those features are mirrored in the DbContext API. In other cases, the Entity Framework team has simplified more complex coding by providing us with methods like Find or properties like DbSet.Local. But there’s a big API lurking underneath that you may still need access to. For example, you might want to work directly with the MetadataWorkspace to write generic code against classes because that API can read the model more efficiently than reflection. Additionally, the MetadataWorkspace is able to provide more information about the metadata than you can discover with reflection, for example, for Key properties. Or you might want to take advantage of a database-specific function that is exposed through Entity SQL, which you can’t access from LINQ to Entities. Or you may already have an application written using the ObjectContext and you want to leverage the DbContext in future updates without replacing all of the ObjectContext code.(Reference from Programming DbContext)

like image 27
sushil pandey Avatar answered Oct 17 '22 14:10

sushil pandey