Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a DataContext in LINQ-to-Entities (NOT Linq-to-SQL)?

I recently asked a question about tracing Linq-to-Entities

I think that one of the answers was not right, as they refer to using the DataContext. Is there a DataContext for LINQ-to-Entities? If so, how do I get it?

like image 223
Phobis Avatar asked Sep 27 '08 02:09

Phobis


People also ask

What is DataContext in LINQ?

Remarks. The DataContext is the source of all entities mapped over a database connection. It tracks changes that you made to all retrieved entities and maintains an "identity cache" that guarantees that entities retrieved more than one time are represented by using the same object instance.

How to add LINQ to SQL classes?

To install the LINQ to SQL tools, start the Visual Studio installer, choose Modify, then select the Individual Components tab, and then select LINQ to SQL tools under the Code Tools category.


1 Answers

LINQ to Entities uses ObjectContext, not DataContext.

Here is a short description of EF:

LINQ to Entities, the ObjectContext Class, and the Entity Data Model

LINQ to Entities queries use the Object Services infrastructure. The ObjectContext class is the primary class for interacting with an EDM as CLR objects. The developer constructs an ObjectQuery instance through the ObjectContext. The generic ObjectQuery class represents a query that returns an instance or collection of typed entities. Entity objects returned by ObjectQuery are tracked by the Object Context and can be updated by using the SaveChanges method.

It doesn't even work the same way as the DataContext in LINQ to SQL. While it is true that they both manage the connection and track changes, yet they differ in how they model the data structures and relationships.

I would give the poster of that wrong answer some slack, though, because LINQ to SQL does make reference to "entities", and someone not familiar with EF could very well still be thinking they know what you are talking about.

For example:

LINQ to SQL and the DataContext Class

The DataContext is the source of all entities mapped over a database connection. It tracks changes that you made to all retrieved entities and maintains an "identity cache" that guarantees that entities retrieved more than one time are represented by using the same object instance.

It can be confusing.

like image 113
hurst Avatar answered Oct 05 '22 23:10

hurst