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, IInfrastructure<IServiceProvider>
Inheritance
DataContext
Implements
Inherited Members
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(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.

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

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

KeepConnectionAlive

Gets flag indicating wether context should dispose underlying connection after use or not. 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.

AddMappingSchema(MappingSchema)

Adds mapping schema to current context.

public void AddMappingSchema(MappingSchema mappingSchema)

Parameters

mappingSchema MappingSchema

Mapping schema to add.

BeginTransaction()

Starts new transaction for current context with default isolation level. If connection already has transaction, it will throw InvalidOperationException.

public virtual DataContextTransaction BeginTransaction()

Returns

DataContextTransaction

Database transaction object.

Exceptions

InvalidOperationException

Thrown when connection already has a transaction.

BeginTransaction(IsolationLevel)

Starts new transaction for current context with specified isolation level. If connection already has transaction, it will throw InvalidOperationException.

public virtual DataContextTransaction BeginTransaction(IsolationLevel level)

Parameters

level IsolationLevel

Transaction isolation level.

Returns

DataContextTransaction

Database transaction object.

Exceptions

InvalidOperationException

Thrown when connection already has a transaction.

BeginTransactionAsync(IsolationLevel, CancellationToken)

Starts new transaction asynchronously for current context with specified isolation level. If connection already has transaction, it will throw InvalidOperationException.

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.

Exceptions

InvalidOperationException

Thrown when connection already has a transaction.

BeginTransactionAsync(CancellationToken)

Starts new transaction asynchronously for current context with default isolation level. If connection already has transaction, it will throw InvalidOperationException.

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

Parameters

cancellationToken CancellationToken

Asynchronous operation cancellation token.

Returns

Task<DataContextTransaction>

Database transaction object.

Exceptions

InvalidOperationException

Thrown when connection already has a transaction.

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()

public void Dispose()

Dispose(bool)

Closes underlying connection.

protected virtual void Dispose(bool disposing)

Parameters

disposing bool

DisposeAsync()

public ValueTask DisposeAsync()

Returns

ValueTask

DisposeAsync(bool)

Closes underlying connection.

protected virtual ValueTask DisposeAsync(bool disposing)

Parameters

disposing bool

Returns

ValueTask

RemoveInterceptor(IInterceptor)

Removes interceptor instance from context.

public void RemoveInterceptor(IInterceptor interceptor)

Parameters

interceptor IInterceptor

Interceptor.

ResetCommandTimeout()

Resets command timeout to provider or connection defaults. Note that default provider/connection timeout is not the same value as timeout value you can specify upon context configuration.

public void ResetCommandTimeout()

ResetCommandTimeoutAsync()

Sets command timeout to default connection value. Note that default provider/connection timeout is not the same value as timeout value you can specify upon context configuration.

public ValueTask ResetCommandTimeoutAsync()

Returns

ValueTask

SetKeepConnectionAlive(bool)

Sets connection management behavior to specify if context should dispose underlying connection after use or not.

public void SetKeepConnectionAlive(bool keepAlive)

Parameters

keepAlive bool

SetKeepConnectionAliveAsync(bool)

Sets connection management behavior to specify if context should dispose underlying connection after use or not.

public ValueTask SetKeepConnectionAliveAsync(bool keepAlive)

Parameters

keepAlive bool

Returns

ValueTask

UseMappingSchema(MappingSchema)

Sets new mapping schema for current data context.

Implements the Disposable pattern, which must be used to properly restore previous settings.

public IDisposable? UseMappingSchema(MappingSchema mappingSchema)

Parameters

mappingSchema MappingSchema

Mapping schema to set.

Returns

IDisposable

UseOptions(Func<DataOptions, DataOptions>)

Sets new options for current data context.

Implements the Disposable pattern, which must be used to properly restore previous options.

public IDisposable? UseOptions(Func<DataOptions, DataOptions> optionsSetter)

Parameters

optionsSetter Func<DataOptions, DataOptions>

Options setter function.

Returns

IDisposable

Returns disposable object, which could be used to restore previous options.

Remarks

For ConnectionOptions we update only mapping schema and connection interceptor. Connection string, configuration, data provider, etc. are not updatable.

Exceptions

ArgumentNullException