Is DbContext the same as DataContext?

DbContext is a new class that was added in the recent separate download by EF team. It is currently not part of the core EF 4.0. However DbContext moving forward would be the preferred way to interact with EF.

So how is it different from ObjectContext? Well semantically they are exactly same but they reduced lot of extra noise that ObjectContext had. Like exposing a set required more work, for instance:

public ObjectSet<Customer> Customers
{
    get { return db.CreateObjectSet<Customer>(); }
}

With DbContext you can do:

public DbSet<Customer> Customers { get; set; }

Basically on the ObjectContext, when you do dot (.), everything is just right there which makes the list pretty huge. What the EF team actually wanted to expose on DbContext are entities which are only specific to your domain and rest of ability of the framework is tucked in under different properties. It just makes the programming experience easier.

This means if you are using ObjectContext right now, with a little bit of code, you can easily move to DbContext.

Leave a Comment