Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

entity framework 4.1 objectContext vs dbContext [duplicate]

Possible Duplicate:
ADO.NET DbContext Generator vs. ADO.NET Poco Entity Generator (ObjectContext)

Should I use ObjectContext or DbContext? What's the best way?

like image 843
user669226 Avatar asked Mar 21 '11 10:03

user669226


People also ask

What is difference between DbContext and ObjectContext?

Definition. DBContext is a wrapper of ObjectContext that exposes the most commonly used features of ObjectContext. In contrast, Object Context is a class of the core Entity framework API that allows performing queries and tracking the updates made to a database using strongly typed entity classes.

What is DbContext and DbSet in Entity Framework?

DbContext generally represents a database connection and a set of tables. DbSet is used to represent a table.

What is DbSet and Objectset in Entity Framework?

It discovers entity sets based on DbSet properties defined on the DbContext derived class (or in general, it discovers your model based on your code). ObjectContext does not do any discovery and is not convention based. It just reads your model from csdl, ssdl and msl artifacts.

Which interface you have to implement to get the reference of ObjectContext from DbContext?

If you need to get ObjectContext you can cast your DbContext instance to IObjectContextAdapter interface (it is implemented explicitly) and wrapped ObjectContext instance will be available: ObjectContext context = ((IObjectContextAdapter)db).


2 Answers

I am currently using DbContext in a Database first situation and it is working fine. DbContext is NOT only for Code First development.

DbContext acts like a wrapper around the ObjectContext. Julie Lerman has a nice explanation, how you can access the ObjectContext that is inside of DbContext here. So if you decide to use DbContext, you can still solve things with ObjectContext if you need to.

DbContext simplifies common tasks. One example is the Find() method.

Product p = db.Products.Find(id); 
like image 116
ckonig Avatar answered Sep 20 '22 23:09

ckonig


ObjectContext for version 4.0 when using a designer generated model and DbContext with a 4.1 Code First model.

like image 26
Darren Lewis Avatar answered Sep 20 '22 23:09

Darren Lewis