Phobis
Phobis

Reputation: 7754

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?

Upvotes: 7

Views: 8262

Answers (4)

Amy B
Amy B

Reputation: 110111

Apparently, LinqToEntities uses an ObjectContext instead of DataContext.

It is hilarious that the object team made a DataContext and the data team made an ObjectContext (and on to DataQuery vs ObjectQuery, etc.) "Naming is Hard!"


Update, for .net 4 with EF4.1, you might also be interested in DbContext when working with LinqToEntities. See also.

Upvotes: 5

naspinski
naspinski

Reputation: 34697

I think you might be referring to the ADO.NET Entity Data Model (.edmx file - comparable to a .dbml file).

In VS it is seen in the Add Item->ADO.NET Entity Data Model

Upvotes: 1

Eric Nelson
Eric Nelson

Reputation: 328

There are a lot of these arbitary syntax differences. E.g. SubmitChanges (L2S) and SaveChanges (L2E). However, that would be just the tip of the differences between the two technologies.

Upvotes: 0

hurst
hurst

Reputation: 1740

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.

Upvotes: 10

Related Questions