Possible Duplicate:
ADO.NET DbContext Generator vs. ADO.NET Poco Entity Generator (ObjectContext)
Should I use ObjectContext or DbContext? What's the best way?
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.
DbContext generally represents a database connection and a set of tables. DbSet is used to represent a table.
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.
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).
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);
ObjectContext for version 4.0 when using a designer generated model and DbContext with a 4.1 Code First model.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With