locked
ObjectContext vs DbContext RRS feed

  • Question

  • User-600916856 posted

    can someone give me some idea of the pros and cons of ObjectContext  and DbContext.  What's the best situation to use them?

    I tried google, msdn, and here, but i can't find any clear answer.

    Wednesday, December 28, 2011 4:40 PM

Answers

  • User1492915735 posted

    Hi,

    DbContext is best however you can convert DbContext back to ObjectContext.

    What’s in EF 4.1?

    ADO.NET Entity Framework 4.1 introduces two new features:
    •The DbContext API is a simplified abstraction over ObjectContext and a number of other types that were included in previous releases of the ADO.NET Entity Framework. The DbContext API surface is optimized for common tasks and coding patterns. DbContext can be used with Database First, Model First and Code First development.
    •Code First is a new development pattern for the ADO.NET Entity Framework and provides an alternative to the existing Database First and Model First patterns. Code First is focused around defining your model using C#/VB.NET classes, these classes can then be mapped to an existing database or be used to generate a database schema. Additional configuration can be supplied using Data Annotations or via a fluent API.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, December 28, 2011 4:50 PM
  • User3866881 posted

    Hello Eileithyia:)

    From what msdn says and according to my experience……I think ObjectContext is a "general" data container that manages whole data contents,and DbContext is a sub class that inherits from it。This means for a specific DataTable's model。You can use DbContext;however,once you don't know which table model you will use to do inserting,deleting or updating……or you just want to do a "general EF's CRUD"。

    But according to what you've described——Since you know about 60 tables……DbContext is your choice here at present。You can get enough intellisense to the strong-typed context。

    Best regaurds!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, December 29, 2011 8:18 PM

All replies

  • User1492915735 posted

    Hi,

    DbContext is best however you can convert DbContext back to ObjectContext.

    What’s in EF 4.1?

    ADO.NET Entity Framework 4.1 introduces two new features:
    •The DbContext API is a simplified abstraction over ObjectContext and a number of other types that were included in previous releases of the ADO.NET Entity Framework. The DbContext API surface is optimized for common tasks and coding patterns. DbContext can be used with Database First, Model First and Code First development.
    •Code First is a new development pattern for the ADO.NET Entity Framework and provides an alternative to the existing Database First and Model First patterns. Code First is focused around defining your model using C#/VB.NET classes, these classes can then be mapped to an existing database or be used to generate a database schema. Additional configuration can be supplied using Data Annotations or via a fluent API.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, December 28, 2011 4:50 PM
  • User-600916856 posted

    thanks,

    I'm mostly do Database First project. I think code first is only good for small application.

    I used ObjectContext before. When i work for Deloitte for last couple years, they have thier own dataobject/access framework.  I'm kind of little out dated right now.

    On this new project we have a database that has around 60 tables( and will add more) and alot of data.   I'm not sure if we should use the new code generated DbContext  or stay with  ObjectContext.

    Wednesday, December 28, 2011 5:38 PM
  • User3866881 posted

    Hello Eileithyia:)

    From what msdn says and according to my experience……I think ObjectContext is a "general" data container that manages whole data contents,and DbContext is a sub class that inherits from it。This means for a specific DataTable's model。You can use DbContext;however,once you don't know which table model you will use to do inserting,deleting or updating……or you just want to do a "general EF's CRUD"。

    But according to what you've described——Since you know about 60 tables……DbContext is your choice here at present。You can get enough intellisense to the strong-typed context。

    Best regaurds!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, December 29, 2011 8:18 PM