Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is ObjectContext deprecated in .NET 4.5?

I've been using ObjectContexts for quite a long time now. Now that I've installed VS 2012, for my surprise the Entity Data Model does not have an option for a Code Generation Item that creates ObjectContexts and EntityObjects instead of DbContexts and POCOs.

Is it deprecated now? I see the ObjectContext class is still available, but there's no way I can use it as the output of my EDMXs.

like image 759
André Pena Avatar asked Nov 28 '12 16:11

André Pena


People also ask

What is ObjectContext in entity Framework?

The ObjectContext class is the primary class for interacting with data as objects that are instances of entity types that are defined in a conceptual model. An instance of the ObjectContext class encapsulates the following: A connection to the database, in the form of an EntityConnection object.

How do I use SaveChanges in entity Framework?

The SaveChanges method of the DbContext prepares the Insert , Update & Delete Queries. It does so by tracking the changes to each of the entities' Context is tracking. Whenever we query the database for entities, the Context retrieves them and mark the entity as Unchanged .

What is Scalar property in entity Framework?

Scalar Property: The type of primitive property is called scalar properties. Each scalar property maps to a column in the database table which stores the real data. For example, StudentID, StudentName, DateofBirth, height, and weight are the scalar properties in the Student entity class.

What is a DbContext class?

A DbContext instance represents a combination of the Unit Of Work and Repository patterns such that it can be used to query from a database and group together changes that will then be written back to the store as a unit. DbContext is conceptually similar to ObjectContext.


2 Answers

ObjectContext is not deprecated. Actually DbContext is just a layer on top of ObjectContext. DbContext API has much nicer/smaller API and is easier to work with. Also POCO entities are much cleaner. This was the reason to change code generation. If you still want to use ObjectContext and EntityObject based entities remove *.tt files under the *.edmx file. Double click the edmx file to open it in the designer. Click on the designer surface. In the properties change "Code Generation Strategy" from "None" to "Default".

EDIT

The behavior has changed a bit in the EF6 designer that ships with VS2013 (and is available for download for VS2012). If you are using EF5 the two options are: 'T4' and 'LegacyObjectContext'. If you are using EF6 the dropdown is disabled. If you would like to use ObjectContext in an EF6 app you need to use Entity Object generator for EF6 (note this link points just to one version of the template but there are actually a few versions depending on what you need - C# vs. VB.NET, WebSite non-WebSite - here is the list). You can find more details about changes in the EF6 tooling here.

like image 194
Pawel Avatar answered Sep 23 '22 10:09

Pawel


Microsoft recommends the EF5.x DbContext Generator for new projects. It is the default template in VS2012. PreVS2012 users can find it using the ExtensionManager (search for EF5.x) or download it here.

You can of course still generate EntityObjects and ObjectContext by using the EF4.x EntityObject Generatortemplate which can be found here.

How to set things up is described here.

like image 45
tweakch Avatar answered Sep 23 '22 10:09

tweakch