locked
difference between DbContext versus ObjectContext... DbSet, ObjectSet, etc. RRS feed

  • Question

  • I've done a lot of googling, and I haven't found a clear answer to the following -- what is the difference between the two - DbContext and ObjectContext, and when do you use which one, and why? Same  question for things like DbSet, ObjectSet, etc etc.

    I am planning to use Code-First with data annotations.

    Thanks.

    Wednesday, December 15, 2010 9:58 PM

All replies

  • DbContext is a wrapper for ObjectContext. The two main things DbContext contains are:
    - a set of APIs that are easier to use than the APIs exposed by ObjectContext (so called Productivity Improvements API) 
    - APIs allowing using CodeFirst approach (ObjectContext does not allow this)
    Note that eventually DbContext will use the wrapped ObjectContext instance to interact with the database. You can access this ObjectContext instance if needed.

    Pawel

    Thursday, December 16, 2010 12:15 AM
  • Hello,

    I see that we should use DbSet<TEntity> for use Code-First. Rİght?

    But when I use "Poco T4" produce code  with ObjectSet<T>
    For instance: //auto-generated code for Product class

    private ObjectSet<Product> _products;

    ..

    get { return _products  ?? (_products = CreateObjectSet<Product>("Products")); }

    ..

    Something changes?

     

    Nuri

    Friday, December 24, 2010 8:38 AM
  • Nuri,

     

    The POCO T4 Templates were released in conjunction with the version of Entity Framework in .NET 4.0—otherwise known as EF4.  DbContext and its related classes have been released as a preview (currently CTP5) that works on top of EF4.  If you want to use T4 templates that generate a DbContext and use DbSet, then you will need to install CTP5 and use the templates from that install.  This blog post covers how to do that: http://blogs.msdn.com/b/adonet/archive/2010/12/06/ef-feature-ctp5-model-amp-database-first-with-dbcontext.aspx

     

    Thanks,

    Arthur

     

    Monday, December 27, 2010 7:16 PM
    Moderator