LINQ-to-Entities中是否存在DataContext(非Linq-to-SQL)?

时间:2022-11-30 17:56:13

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

我最近问了一个关于跟踪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?

我认为其中一个答案是不正确的,因为它们指的是使用DataContext。 LINQ到实体是否有DataContext?如果是这样,我怎么得到它?

4 个解决方案

#1


10  

LINQ to Entities uses ObjectContext, not DataContext.

LINQ to Entities使用ObjectContext,而不是DataContext。

Here is a short description of EF:

以下是EF的简短描述:

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

LINQ to Entities,ObjectContext类和实体数据模型

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.

LINQ to Entities查询使用对象服务基础结构。 ObjectContext类是与EDM作为CLR对象进行交互的主要类。开发人员通过ObjectContext构造ObjectQuery实例。通用ObjectQuery类表示返回实例或类型化实体集合的查询。 ObjectQuery返回的实体对象由对象上下文跟踪,可以使用SaveChanges方法进行更新。

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.

它甚至与LINQ to SQL中的DataContext的工作方式不同。虽然它们都管理连接并跟踪更改,但它们对数据结构和关系的建模方式不同。

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.

不过,我会给出错误答案的海报,因为LINQ to SQL确实引用了“实体”,不熟悉EF的人很可能仍然认为他们知道你在说什么。

For example:

LINQ to SQL and the DataContext Class

LINQ to SQL和DataContext类

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.

DataContext是通过数据库连接映射的所有实体的源。它跟踪您对所有检索到的实体所做的更改,并维护“身份缓存”,以确保通过使用相同的对象实例来表示检索多次的实体。

It can be confusing.

这可能令人困惑。

#2


5  

Apparently, LinqToEntities uses an ObjectContext instead of DataContext.

显然,LinqToEntities使用ObjectContext而不是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!"

令人高兴的是,对象团队制作了一个DataContext,数据团队制作了一个ObjectContext(以及DataQuery vs ObjectQuery等)“Naming很难!”


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

使用EF4.1更新.net 4,在使用LinqToEntities时,您可能也对DbContext感兴趣。也可以看看。

#3


1  

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

我想你可能指的是ADO.NET实体数据模型(.edmx文件 - 与.dbml文件相当)。

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

在VS中,可以在Add Item-> ADO.NET Entity Data Model中看到它

#4


0  

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.

这些仲裁语法有很多不同之处。例如。 SubmitChanges(L2S)和SaveChanges(L2E)。然而,这只是两种技术之间差异的一小部分。

#1


10  

LINQ to Entities uses ObjectContext, not DataContext.

LINQ to Entities使用ObjectContext,而不是DataContext。

Here is a short description of EF:

以下是EF的简短描述:

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

LINQ to Entities,ObjectContext类和实体数据模型

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.

LINQ to Entities查询使用对象服务基础结构。 ObjectContext类是与EDM作为CLR对象进行交互的主要类。开发人员通过ObjectContext构造ObjectQuery实例。通用ObjectQuery类表示返回实例或类型化实体集合的查询。 ObjectQuery返回的实体对象由对象上下文跟踪,可以使用SaveChanges方法进行更新。

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.

它甚至与LINQ to SQL中的DataContext的工作方式不同。虽然它们都管理连接并跟踪更改,但它们对数据结构和关系的建模方式不同。

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.

不过,我会给出错误答案的海报,因为LINQ to SQL确实引用了“实体”,不熟悉EF的人很可能仍然认为他们知道你在说什么。

For example:

LINQ to SQL and the DataContext Class

LINQ to SQL和DataContext类

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.

DataContext是通过数据库连接映射的所有实体的源。它跟踪您对所有检索到的实体所做的更改,并维护“身份缓存”,以确保通过使用相同的对象实例来表示检索多次的实体。

It can be confusing.

这可能令人困惑。

#2


5  

Apparently, LinqToEntities uses an ObjectContext instead of DataContext.

显然,LinqToEntities使用ObjectContext而不是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!"

令人高兴的是,对象团队制作了一个DataContext,数据团队制作了一个ObjectContext(以及DataQuery vs ObjectQuery等)“Naming很难!”


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

使用EF4.1更新.net 4,在使用LinqToEntities时,您可能也对DbContext感兴趣。也可以看看。

#3


1  

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

我想你可能指的是ADO.NET实体数据模型(.edmx文件 - 与.dbml文件相当)。

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

在VS中,可以在Add Item-> ADO.NET Entity Data Model中看到它

#4


0  

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.

这些仲裁语法有很多不同之处。例如。 SubmitChanges(L2S)和SaveChanges(L2E)。然而,这只是两种技术之间差异的一小部分。