DataContext 类

定义

表示 LINQ to SQL 框架的主入口点。Represents the main entry point for the LINQ to SQL framework.

public ref class DataContext : IDisposable
public class DataContext : IDisposable
type DataContext = class
    interface IDisposable
Public Class DataContext
Implements IDisposable
继承
DataContext
实现

注解

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实例设计为最后一个 "工作单元",但您的应用程序定义了该术语。In general, a DataContext instance is designed to last for one "unit of work" however your application defines that term. DataContext轻量的,创建的成本并不高。A DataContext is lightweight and is not expensive to create. 典型LINQ to SQLLINQ to SQL的应用程序DataContext在方法范围创建实例,或在表示一组相关数据库操作逻辑集的生存期类的成员中创建实例。A typical LINQ to SQLLINQ to SQL application creates DataContext instances at method scope or as a member of short-lived classes that represent a logical set of related database operations.

构造函数

DataContext(IDbConnection)

通过引用由 .NET Framework.NET Framework 使用的连接来初始化 DataContext 类的新实例。Initializes a new instance of the DataContext class by referencing the connection used by the .NET Framework.NET Framework.

DataContext(IDbConnection, MappingSource)

通过引用连接和映射源初始化 DataContext 类的新实例。Initializes a new instance of the DataContext class by referencing a connection and a mapping source.

DataContext(String)

通过引用文件源来初始化 DataContext 类的新实例。Initializes a new instance of the DataContext class by referencing a file source.

DataContext(String, MappingSource)

通过引用文件源和映射源初始化 DataContext 类的新实例。Initializes a new instance of the DataContext class by referencing a file source and a mapping source.

属性

ChangeConflicts

获取调用 SubmitChanges() 时导致并发冲突的对象的集合。Gets a collection of objects that caused concurrency conflicts when SubmitChanges() was called.

CommandTimeout

获取或设置一个值,该值增大查询的超时期限(以秒为单位),如果不增大则会在默认超时期限间出现超时。Gets or sets a value that increases the time-out period (in seconds) for queries that would otherwise time out during the default time-out period.

Connection

获取由框架使用的连接。Gets the connection used by the framework.

DeferredLoadingEnabled

获取或设置一个值,该值指示延迟加载是一对多还是一对一的关系。Gets or sets a value that indicates whether to delay-load one-to-many or one-to-one relationships.

LoadOptions

获取或设置与此 DataContext 关联的 DataLoadOptionsGets or sets the DataLoadOptions associated with this DataContext.

Log

获取或设置要写入 SQL 查询或命令的目标。Gets or sets the destination to write the SQL query or command.

Mapping

获取映射所基于的 MetaModelGets the MetaModel on which the mapping is based.

ObjectTrackingEnabled

获取或设置一个值,该值指示是否启用对象跟踪。Gets or sets a value that indicates whether object tracking is enabled.

QueryCacheEnabled
Transaction

获取或设置要用于访问数据库的 .NET Framework.NET Framework 的本地事务。Gets or sets a local transaction for the .NET Framework.NET Framework to use to access the database.

方法

CreateDatabase()

在服务器上创建数据库。Creates a database on the server.

CreateMethodCallQuery<TResult>(Object, MethodInfo, Object[])

执行与指定 .NET 方法关联的表值数据库函数。Executes the table-valued database function associated with the specified .NET method.

DatabaseExists()

确定是否可以打开关联数据库。Determines whether the associated database can be opened.

DeleteDatabase()

删除关联数据库。Deletes the associated database.

Dispose()

释放 DataContext 类的当前实例所使用的所有资源。Releases all resources used by the current instance of the DataContext class.

Dispose(Boolean)

释放 DataContext 类使用的非托管资源,还可以选择释放托管资源。Releases the unmanaged resources used by the DataContext class and optionally releases the managed resource.

Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)
ExecuteCommand(String, Object[])

直接对数据库执行 SQL 命令。Executes SQL commands directly on the database.

ExecuteDynamicDelete(Object)

在删除重写方法中执行,以向 LINQ to SQLLINQ to SQL 重新委托生成和执行删除操作的动态 SQL 的任务。Executes, inside delete override methods, to redelegate to LINQ to SQLLINQ to SQL the task of generating and executing dynamic SQL for delete operations.

ExecuteDynamicInsert(Object)

在插入重写方法中执行,以向 LINQ to SQLLINQ to SQL 重新委托生成和执行插入操作的动态 SQL 的任务。Executes, inside insert override methods, to redelegate to LINQ to SQLLINQ to SQL the task of generating and executing dynamic SQL for insert operations.

ExecuteDynamicUpdate(Object)

在更新重写方法中执行,以向 LINQ to SQLLINQ to SQL 重新委托生成和执行更新操作的动态 SQL 的任务。Executes, inside update override methods, to redelegate to LINQ to SQLLINQ to SQL the task of generating and executing dynamic SQL for update operations.

ExecuteMethodCall(Object, MethodInfo, Object[])

执行与指定 .NET 方法关联的数据库存储过程或标量函数。Executes the stored database procedure or scalar function associated with the specified .NET method.

ExecuteQuery(Type, String, Object[])

直接对数据库执行 SQL 查询。Executes SQL queries directly on the database.

ExecuteQuery<TResult>(String, Object[])

直接对数据库执行 SQL 查询并返回对象。Executes SQL queries directly on the database and returns objects.

GetChangeSet()

获取由 DataContext跟踪的被修改对象。Gets the modified objects tracked by DataContext.

GetCommand(IQueryable)

获取有关由 LINQ to SQLLINQ to SQL 生成的 SQL 命令的信息。Gets the information about SQL commands generated by LINQ to SQLLINQ to SQL.

GetHashCode()

作为默认哈希函数。Serves as the default hash function.

(继承自 Object)
GetIDbCommand(IQueryable)
GetTable(Type)

返回特定类型的对象的集合,其中类型由 type 参数定义。Returns a collection of objects of a particular type, where the type is defined by the type parameter.

GetTable<TEntity>()

返回特定类型的对象的集合,其中类型由 TEntity 参数定义。Returns a collection of objects of a particular type, where the type is defined by the TEntity parameter.

GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
Refresh(RefreshMode, IEnumerable)

按照指定模式刷新实体对象的集合。Refreshes a collection of entity objects according to the specified mode.

Refresh(RefreshMode, Object)

按照指定模式刷新实体对象。Refreshes an entity object according to the specified mode.

Refresh(RefreshMode, Object[])

按照指定模式刷新实体对象的数组。Refreshes an array of entity objects according to the specified mode.

SubmitChanges()

计算要插入、更新或删除的已修改对象的集,并执行相应命令以实现对数据库的更改。Computes the set of modified objects to be inserted, updated, or deleted, and executes the appropriate commands to implement the changes to the database.

SubmitChanges(ConflictMode)

将对检索到的对象所做的更改发送到基础数据库,并指定提交失败时要采取的操作。Sends changes that were made to retrieved objects to the underlying database, and specifies the action to be taken if the submission fails.

ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(继承自 Object)
Translate(DbDataReader)

将现有 DbDataReader 转换为对象。Converts an existing DbDataReader to objects.

Translate(Type, DbDataReader)

将现有 DbDataReader 转换为对象。Converts an existing DbDataReader to objects.

Translate<TResult>(DbDataReader)

将现有 DbDataReader 转换为对象。Converts an existing DbDataReader to objects.

适用于