LINQ-to-Entities是否有DataContext(不是Linq-to-SQL)?
时间:2020-03-06 14:48:59 来源:igfitidea点击:
我最近问了一个有关跟踪Linq-to-Entities的问题
我认为答案之一是不正确的,因为它们指的是使用DataContext。是否有用于LINQ到实体的DataContext?如果是这样,我如何得到它?
解决方案
显然,LinqToEntities使用ObjectContext而不是DataContext。
有趣的是,对象团队创建了一个DataContext,数据团队创建了一个ObjectContext(以及DataQuery和ObjectQuery等)。
更新,对于使用EF4.1的.net 4,在使用LinqToEntities时,我们可能也对DbContext感兴趣。也可以看看。
LINQ to Entities使用ObjectContext,而不是DataContext。
以下是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.
它甚至与LINQ to SQL中的DataContext的工作方式不同。
虽然它们既可以管理连接并跟踪更改,但它们在建模数据结构和关系的方式上却有所不同。
但是,我会给错误答案的发布者一些懈怠,因为LINQ to SQL确实引用了"实体",并且不熟悉EF的人很可能仍会以为他们知道我们在说什么。
例如:
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.
可能会造成混乱。
这些蛮横的语法差异很多。例如。 SubmitChanges(L2S)和SaveChanges(L2E)。但是,那仅仅是两种技术之间差异的提示。
我认为我们可能是指ADO.NET实体数据模型(相当于.dbml文件的.edmx文件)。
在VS中,可以在"添加项目"->" ADO.NET实体数据模型"中看到