在LINQ-to-Entities中有一个DataContext(不是Linq-to-SQL)吗? [英] Is there a DataContext in LINQ-to-Entities (NOT Linq-to-SQL)?

查看:72
本文介绍了在LINQ-to-Entities中有一个DataContext(不是Linq-to-SQL)吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近问了一个有关跟踪Linq-to-Entities

的问题>

我认为答案之一是不正确的,因为它们请参阅使用DataContext.是否有用于LINQ到实体的DataContext?如果是这样,我怎么得到它?

解决方案

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

以下是EF的简短说明:

LINQ到实体,ObjectContext类和实体数据模型

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

它甚至与LINQ to SQL中的DataContext的工作方式不同. 虽然它们既可以管理连接并可以跟踪更改,但是在建模数据结构和关系的方式上却有所不同.

不过,我会给这个错误答案的发布者一些懈怠,因为LINQ to SQL确实引用了实体",而且不熟悉EF的人很可能仍会以为他们知道您在说什么. /p>

例如:

LINQ to SQL and DataContext类

DataContext是映射在数据库上的所有实体的 联系.它跟踪您对所有检索到的实体所做的更改, 维护一个身份缓存",以确保检索到的实体 使用相同的对象实例表示一次以上.

这可能会令人困惑.

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?

解决方案

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.

这篇关于在LINQ-to-Entities中有一个DataContext(不是Linq-to-SQL)吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆