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
DataOptionsOptions, 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
IDataProviderDatabase provider implementation.
connectionString
stringDatabase connection string.
DataContext(string?)
Creates data context using specific database configuration.
public DataContext(string? configurationString)
Parameters
configurationString
stringConnection 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
stringName of database provider to use with this connection. ProviderName class for list of providers.
connectionString
stringDatabase 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
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
ConfigurationID
Gets or sets ContextID.
public int ConfigurationID { get; }
Property Value
ConfigurationString
Gets initial value for database connection configuration name.
public string? ConfigurationString { get; }
Property Value
ConnectionString
Gets initial value for database connection string.
public string? ConnectionString { get; }
Property Value
ContextName
Gets or sets context identifier. Uses provider's name by default.
public string ContextName { get; }
Property Value
DataProvider
Gets database provider implementation.
public IDataProvider DataProvider { get; }
Property Value
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
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
KeepConnectionAlive
Gets or sets option to dispose underlying connection after use.
Default value: false
.
public bool KeepConnectionAlive { get; set; }
Property Value
LastQuery
Contains text of last command, sent to database using current context.
public string? LastQuery { get; set; }
Property Value
MappingSchema
Gets or sets mapping schema. Uses provider's mapping schema by default.
public MappingSchema MappingSchema { get; }
Property Value
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
OnTraceConnection
Gets or sets trace handler, used for data connection instance.
public Action<TraceInfo>? OnTraceConnection { get; set; }
Property Value
Options
Current DataContext options
public DataOptions Options { get; }
Property Value
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
Methods
AddInterceptor(IInterceptor)
Adds interceptor instance to context.
public void AddInterceptor(IInterceptor interceptor)
Parameters
interceptor
IInterceptorInterceptor.
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
IsolationLevelTransaction 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
IsolationLevelTransaction isolation level.
cancellationToken
CancellationTokenAsynchronous 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
CancellationTokenAsynchronous 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.
protected virtual DataConnection CloneDataConnection(DataConnection currentConnection, DataOptions options)
Parameters
currentConnection
DataConnectionoptions
DataOptions
Returns
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
RemoveInterceptor(IInterceptor)
Removes interceptor instance from context.
public void RemoveInterceptor(IInterceptor interceptor)
Parameters
interceptor
IInterceptorInterceptor.