When mapping database tables using ADO.NET Entity datamodel, why does Entity Framework utilize the ObjectContext instead of the DbContext?

asp.net asp.net-mvc-3 dbcontext entity-framework objectcontext


I generated the tables in my SQL Server 2008 database using the database technique, and then I used an ADO.NET Entity Data Model to link those tables to Entity Framework classes. However, I discovered the following code in the automatically generated class definition when I opened the designer.cs file:

public partial class PortalEntities : ObjectContext

I am thus perplexed by the following three questions:

  1. How come myPortalEntities class comes fromObjectContext but notDbContext as I had anticipated?

  2. Is there a significant difference betweenObjectContext & DbContext or they provide the same qualities and are largely the same.

  3. When I attempt to create code like the following:

    Student student = db.Students.Find(id);

I discovered that I'm unable to.Find() approach I once employedDbContext does this imply thatObjectContext & DbContext have several techniques I may employ?


1/26/2012 3:53:36 PM

Accepted Answer

The DbContext a covering placed over theObjectContext It makes the interface simpler for the tasks we do most often.

If you own anDbContext You still have access to theObjectContext trough ((IObjectContextAdapter)dbContext).ObjectContext;

When using theDbContext as opposed to theObjectContext You may change the template that is used to generate your code when utilizing database first. To do this, just right-click on your EDMX and choose "Add Code Generation Item." The DbContext template is then available for choice.

34 zzzs of the whole procedure.

11/10/2012 7:44:34 AM

Popular Answer

The default code generation in VS2012 changed fromObjectContext to DbContext .

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow