Is DbContext the same as DataContext?

C#DatabaseEntity Framework-4

C# Problem Overview


I'm following a tutorial by Scott Gu that refers to a class named DbContext. I can't find it on any namespace on framework 4 and it seems to me it was renamed from CT4 DbContext to .net4 System.Data.Linq.DataContext. Is my assumption correct?

C# Solutions


Solution 1 - C#

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.

Solution 2 - C#

It's a bit too late, but for the googlers. DbContext is used for EF (EntityFramework) and DataContext is used for L2S (LINQ To SQL).

Solution 3 - C#

DbContext

  • one of EntityFramework's classes.
  • represents a Session between your program & a Database.
  • allows your program to send & retrieve data to/from a Database.

DataContext

  • a class you create in your program that inherits from DbContext.
  • use DataContext to retrieve or update data locally in your program.
  • then push changes (using methods from the inherited DbContext) to the actual Database to update it.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPadu MerlotiView Question on Stackoverflow
Solution 1 - C#zeeshanhiraniView Answer on Stackoverflow
Solution 2 - C#Dmitrij KultasevView Answer on Stackoverflow
Solution 3 - C#MiminaView Answer on Stackoverflow