Table of Contents

Class DataContext

Namespace
LinqToDB
Assembly
linq2db.dll

Implements abstraction over non-persistent database connection that could be released after query or transaction execution.

public class DataContext : IDataContext, IConfigurationID, IDisposable, IAsyncDisposable, IInterceptable<ICommandInterceptor>, IInterceptable<IConnectionInterceptor>, IInterceptable<IDataContextInterceptor>, IInterceptable<IEntityServiceInterceptor>, IInterceptable<IUnwrapDataObjectInterceptor>, IInterceptable
Inheritance
DataContext
Implements
Extension Methods

Constructors

DataContext()

Creates data context using default database configuration. DefaultConfiguration for more details.

public DataContext()

DataContext(DataOptions)

Creates database context object that uses a DataOptions to configure the connection.

public DataContext(DataOptions options)

Parameters

options DataOptions

Options, setup ahead of time.

DataContext(IDataProvider, string)

Creates data context using specific data provider implementation and connection string.

public DataContext(IDataProvider dataProvider, string connectionString)

Parameters

dataProvider IDataProvider

Database provider implementation.

connectionString string

Database connection string.

DataContext(string?)

Creates data context using specific database configuration.

public DataContext(string? configurationString)

Parameters

configurationString string

Connection configuration name. In case of null value, context will use default configuration. DefaultConfiguration for more details.

DataContext(string, string)

Creates data context using specified database provider and connection string.

public DataContext(string providerName, string connectionString)

Parameters

providerName string

Name of database provider to use with this connection. ProviderName class for list of providers.

connectionString string

Database connection string to use for connection with database.

Properties

CloseAfterUse

Gets or sets flag to close context after query execution or leave it open.

public bool CloseAfterUse { get; set; }

Property Value

bool

CommandTimeout

Gets or sets command execution timeout in seconds. Negative timeout value means that default timeout will be used. 0 timeout value corresponds to infinite timeout. By default timeout is not set and default value for current provider used.

public int CommandTimeout { get; set; }

Property Value

int

ConfigurationID

Gets or sets ContextID.

public int ConfigurationID { get; }

Property Value

int

ConfigurationString

Gets initial value for database connection configuration name.

public string? ConfigurationString { get; }

Property Value

string

ConnectionString

Gets initial value for database connection string.

public string? ConnectionString { get; }

Property Value

string

ContextName

Gets or sets context identifier. Uses provider's name by default.

public string ContextName { get; }

Property Value

string

DataProvider

Gets database provider implementation.

public IDataProvider DataProvider { get; }

Property Value

IDataProvider

InlineParameters

Gets or sets option to force inline parameter values as literals into command text. If parameter inlining not supported for specific value type, it will be used as parameter.

public bool InlineParameters { get; set; }

Property Value

bool

IsMarsEnabled

Gets or sets status of Multiple Active Result Sets (MARS) feature. This feature available only for SQL Azure and SQL Server 2005+.

public bool IsMarsEnabled { get; set; }

Property Value

bool

KeepConnectionAlive

Gets or sets option to dispose underlying connection after use. Default value: false.

public bool KeepConnectionAlive { get; set; }

Property Value

bool

LastQuery

Contains text of last command, sent to database using current context.

public string? LastQuery { get; set; }

Property Value

string

MappingSchema

Gets or sets mapping schema. Uses provider's mapping schema by default.

public MappingSchema MappingSchema { get; }

Property Value

MappingSchema

NextQueryHints

Gets list of query hints (writable collection), that will be used only for next query, executed through current context.

public List<string> NextQueryHints { get; }

Property Value

List<string>

OnTraceConnection

Gets or sets trace handler, used for data connection instance.

public Action<TraceInfo>? OnTraceConnection { get; set; }

Property Value

Action<TraceInfo>

Options

Current DataContext options

public DataOptions Options { get; }

Property Value

DataOptions

QueryHints

Gets list of query hints (writable collection), that will be used for all queries, executed through current context.

public List<string> QueryHints { get; }

Property Value

List<string>

Methods

AddInterceptor(IInterceptor)

Adds interceptor instance to context.

public void AddInterceptor(IInterceptor interceptor)

Parameters

interceptor IInterceptor

Interceptor.

BeginTransaction()

Starts new transaction for current context with default isolation level. If connection already has transaction, it will be rolled back.

public virtual DataContextTransaction BeginTransaction()

Returns

DataContextTransaction

Database transaction object.

BeginTransaction(IsolationLevel)

Starts new transaction for current context with specified isolation level. If connection already has transaction, it will be rolled back.

public virtual DataContextTransaction BeginTransaction(IsolationLevel level)

Parameters

level IsolationLevel

Transaction isolation level.

Returns

DataContextTransaction

Database transaction object.

BeginTransactionAsync(IsolationLevel, CancellationToken)

Starts new transaction asynchronously for current context with specified isolation level. If connection already has transaction, it will be rolled back.

public virtual Task<DataContextTransaction> BeginTransactionAsync(IsolationLevel level, CancellationToken cancellationToken = default)

Parameters

level IsolationLevel

Transaction isolation level.

cancellationToken CancellationToken

Asynchronous operation cancellation token.

Returns

Task<DataContextTransaction>

Database transaction object.

BeginTransactionAsync(CancellationToken)

Starts new transaction asynchronously for current context with default isolation level. If connection already has transaction, it will be rolled back.

public virtual Task<DataContextTransaction> BeginTransactionAsync(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken

Asynchronous operation cancellation token.

Returns

Task<DataContextTransaction>

Database transaction object.

CloneDataConnection(DataConnection, DataOptions)

Creates instance of DataConnection class, attached to same database connection/transaction passed in options. Used by Clone(bool) API only if IsMarsEnabled is true and there is an active connection associated with current context.

DataConnection instance, used by current context instance.Connection options, will have DbConnection or DbTransaction set.New DataConnection instance.
protected virtual DataConnection CloneDataConnection(DataConnection currentConnection, DataOptions options)

Parameters

currentConnection DataConnection
options DataOptions

Returns

DataConnection

CreateDataConnection(DataOptions)

Creates instance of DataConnection class, used by context internally.

protected virtual DataConnection CreateDataConnection(DataOptions options)

Parameters

options DataOptions

Returns

DataConnection

New DataConnection instance.

Dispose(bool)

Closes underlying connection.

protected virtual void Dispose(bool disposing)

Parameters

disposing bool

DisposeAsync(bool)

Closes underlying connection.

protected virtual Task DisposeAsync(bool disposing)

Parameters

disposing bool

Returns

Task

RemoveInterceptor(IInterceptor)

Removes interceptor instance from context.

public void RemoveInterceptor(IInterceptor interceptor)

Parameters

interceptor IInterceptor

Interceptor.