Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'ObjectContext' vs 'DbContext' in Entity Framework

I'm using the DbContext class within code that I am creating that is based on the Generic Repositories and Unit of Work design patterns. (I am following the guidance here.) While working on this project I have encountered the ObjectContext class.

I've read quite a number of posts that discuss ObjectContext vs. DbContext. While some of what I've read makes sense, I still don't have a complete understanding of the differences and this leaves me wondering about my current implementation. Should I be using DbContext, ObjectContext or both? Is using one of these now considered an anti-pattern?

like image 765
afr0 Avatar asked Feb 07 '12 13:02

afr0


People also ask

What is the difference between DbContext and ObjectContext?

Dbcontext can be defined as a lightweight version of the ObjectContext or we can say Dbcontext is a wrapper of ObjectContext and exposes only the common features that are really required in programming.

What is the difference between DbContext and DbSet?

Intuitively, a DbContext corresponds to your database (or a collection of tables and views in your database) whereas a DbSet corresponds to a table or view in your database.

What is the use of DbContext in Entity Framework?

A DbContext instance represents a combination of the Unit Of Work and Repository patterns such that it can be used to query from a database and group together changes that will then be written back to the store as a unit. DbContext is conceptually similar to ObjectContext.

Does DB context inherit ObjectContext?

DbContext is nothing but a ObjectContext wrapper, we can say it is a lightweight alternative to the ObjectContext. DbContext can be used for DataBase first, code first and model first development. DbContext mainly contains a set of APIs that are very easy to use. The API is exposed by ObjectContext.


1 Answers

DbContext is just a wrapper around ObjectContext.

DbContext is just a set of APIs that are easier to use than the APIs exposed by ObjectContext.

Anyway, here you'll find a very simple Visual Studio template that uses the Repository Pattern and the Entity Framework.

like image 82
Massimiliano Peluso Avatar answered Oct 12 '22 13:10

Massimiliano Peluso