Class DataContext
Implements abstraction over non-persistent database connection that could be released after query or transaction execution.
Inheritance
System.Object
DataContext
Assembly: linq2db.dll
Syntax
public class DataContext : object, IDataContext, IConfigurationID, IAsyncDisposable, IInterceptable<ICommandInterceptor>, IInterceptable<IConnectionInterceptor>, IInterceptable<IDataContextInterceptor>, IInterceptable<IEntityServiceInterceptor>, IInterceptable<IUnwrapDataObjectInterceptor>, IInterceptable
Constructors
|
Improve this Doc
View Source
DataContext()
Declaration
|
Improve this Doc
View Source
DataContext(DataOptions)
Creates database context object that uses a DataOptions to configure the connection.
Declaration
public DataContext(DataOptions options)
Parameters
Type |
Name |
Description |
DataOptions |
options |
Options, setup ahead of time.
|
|
Improve this Doc
View Source
DataContext(IDataProvider, String)
Creates data context using specific data provider implementation and connection string.
Declaration
public DataContext(IDataProvider dataProvider, string connectionString)
Parameters
Type |
Name |
Description |
IDataProvider |
dataProvider |
Database provider implementation.
|
System.String |
connectionString |
Database connection string.
|
|
Improve this Doc
View Source
DataContext(Nullable<String>)
Creates data context using specific database configuration.
Declaration
public DataContext(string? configurationString)
Parameters
Type |
Name |
Description |
System.Nullable<System.String> |
configurationString |
Connection configuration name.
In case of null value, context will use default configuration.
DefaultConfiguration for more details.
|
|
Improve this Doc
View Source
DataContext(String, String)
Creates data context using specified database provider and connection string.
Declaration
public DataContext(string providerName, string connectionString)
Parameters
Type |
Name |
Description |
System.String |
providerName |
Name of database provider to use with this connection. ProviderName class for list of providers.
|
System.String |
connectionString |
Database connection string to use for connection with database.
|
Properties
|
Improve this Doc
View Source
CloseAfterUse
Gets or sets flag to close context after query execution or leave it open.
Declaration
public bool CloseAfterUse { get; set; }
Property Value
Type |
Description |
System.Boolean |
|
|
Improve this Doc
View Source
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.
Declaration
public int CommandTimeout { get; set; }
Property Value
Type |
Description |
System.Int32 |
|
|
Improve this Doc
View Source
ConfigurationID
Declaration
public int ConfigurationID { get; }
Property Value
Type |
Description |
System.Int32 |
|
|
Improve this Doc
View Source
ConfigurationString
Gets initial value for database connection configuration name.
Declaration
public string? ConfigurationString { get; }
Property Value
Type |
Description |
System.Nullable<System.String> |
|
|
Improve this Doc
View Source
ConnectionString
Gets initial value for database connection string.
Declaration
public string? ConnectionString { get; }
Property Value
Type |
Description |
System.Nullable<System.String> |
|
|
Improve this Doc
View Source
ContextName
Gets or sets context identifier. Uses provider's name by default.
Declaration
public string ContextName { get; }
Property Value
Type |
Description |
System.String |
|
|
Improve this Doc
View Source
DataProvider
Gets database provider implementation.
Declaration
public IDataProvider DataProvider { get; }
Property Value
|
Improve this Doc
View Source
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.
Declaration
public bool InlineParameters { get; set; }
Property Value
Type |
Description |
System.Boolean |
|
|
Improve this Doc
View Source
IsMarsEnabled
Gets or sets status of Multiple Active Result Sets (MARS) feature. This feature available only for
SQL Azure and SQL Server 2005+.
Declaration
public bool IsMarsEnabled { get; set; }
Property Value
Type |
Description |
System.Boolean |
|
|
Improve this Doc
View Source
KeepConnectionAlive
Gets or sets option to dispose underlying connection after use.
Default value: false
.
Declaration
public bool KeepConnectionAlive { get; set; }
Property Value
Type |
Description |
System.Boolean |
|
|
Improve this Doc
View Source
LastQuery
Contains text of last command, sent to database using current context.
Declaration
public string? LastQuery { get; set; }
Property Value
Type |
Description |
System.Nullable<System.String> |
|
|
Improve this Doc
View Source
MappingSchema
Gets or sets mapping schema. Uses provider's mapping schema by default.
Declaration
public MappingSchema MappingSchema { get; }
Property Value
|
Improve this Doc
View Source
NextQueryHints
Gets list of query hints (writable collection), that will be used only for next query, executed through current context.
Declaration
public List<string> NextQueryHints { get; }
Property Value
Type |
Description |
List<System.String> |
|
|
Improve this Doc
View Source
OnTraceConnection
Gets or sets trace handler, used for data connection instance.
Declaration
public Action<TraceInfo>? OnTraceConnection { get; set; }
Property Value
Type |
Description |
System.Nullable<Action<TraceInfo>> |
|
|
Improve this Doc
View Source
Options
Current DataContext options
Declaration
public DataOptions Options { get; }
Property Value
|
Improve this Doc
View Source
QueryHints
Gets list of query hints (writable collection), that will be used for all queries, executed through current context.
Declaration
public List<string> QueryHints { get; }
Property Value
Type |
Description |
List<System.String> |
|
Methods
|
Improve this Doc
View Source
AddInterceptor(IInterceptor)
Adds interceptor instance to context.
Declaration
public void AddInterceptor(IInterceptor interceptor)
Parameters
|
Improve this Doc
View Source
BeginTransaction()
Starts new transaction for current context with default isolation level.
If connection already has transaction, it will be rolled back.
Declaration
public virtual DataContextTransaction BeginTransaction()
Returns
|
Improve this Doc
View Source
BeginTransaction(IsolationLevel)
Starts new transaction for current context with specified isolation level.
If connection already has transaction, it will be rolled back.
Declaration
public virtual DataContextTransaction BeginTransaction(IsolationLevel level)
Parameters
Type |
Name |
Description |
IsolationLevel |
level |
Transaction isolation level.
|
Returns
|
Improve this Doc
View Source
BeginTransactionAsync(CancellationToken)
Starts new transaction asynchronously for current context with default isolation level.
If connection already has transaction, it will be rolled back.
Declaration
public virtual async Task<DataContextTransaction> BeginTransactionAsync(CancellationToken cancellationToken = null)
Parameters
Type |
Name |
Description |
CancellationToken |
cancellationToken |
Asynchronous operation cancellation token.
|
Returns
|
Improve this Doc
View Source
BeginTransactionAsync(IsolationLevel, CancellationToken)
Starts new transaction asynchronously for current context with specified isolation level.
If connection already has transaction, it will be rolled back.
Declaration
public virtual async Task<DataContextTransaction> BeginTransactionAsync(IsolationLevel level, CancellationToken cancellationToken = null)
Parameters
Type |
Name |
Description |
IsolationLevel |
level |
Transaction isolation level.
|
CancellationToken |
cancellationToken |
Asynchronous operation cancellation token.
|
Returns
|
Improve this Doc
View Source
CloneDataConnection(DataConnection, DataOptions)
Creates instance of DataConnection class, attached to same database connection/transaction passed in options.
Used by Clone(Boolean) 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 or set.
New DataConnection instance.
Declaration
protected virtual DataConnection CloneDataConnection(DataConnection currentConnection, DataOptions options)
Parameters
Returns
|
Improve this Doc
View Source
CreateDataConnection(DataOptions)
Declaration
protected virtual DataConnection CreateDataConnection(DataOptions options)
Parameters
Returns
|
Improve this Doc
View Source
Dispose(Boolean)
Closes underlying connection.
Declaration
protected virtual void Dispose(bool disposing)
Parameters
Type |
Name |
Description |
System.Boolean |
disposing |
|
|
Improve this Doc
View Source
DisposeAsync(Boolean)
Closes underlying connection.
Declaration
protected virtual Task DisposeAsync(bool disposing)
Parameters
Type |
Name |
Description |
System.Boolean |
disposing |
|
Returns
|
Improve this Doc
View Source
GetFluentMappingBuilder()
Declaration
public FluentMappingBuilder GetFluentMappingBuilder()
Returns
|
Improve this Doc
View Source
RemoveInterceptor(IInterceptor)
Declaration
public void RemoveInterceptor(IInterceptor interceptor)
Parameters
Explicit Interface Implementations
|
Improve this Doc
View Source
IAsyncDisposable.DisposeAsync()
Declaration
async Task IAsyncDisposable.DisposeAsync()
Returns
|
Improve this Doc
View Source
IDataContext.Clone(Boolean)
Declaration
IDataContext IDataContext.Clone(bool forNestedQuery)
Parameters
Type |
Name |
Description |
System.Boolean |
forNestedQuery |
|
Returns
|
Improve this Doc
View Source
IDataContext.Close()
Declaration
void IDataContext.Close()
|
Improve this Doc
View Source
IDataContext.CloseAsync()
Declaration
async Task IDataContext.CloseAsync()
Returns
|
Improve this Doc
View Source
IDataContext.CreateSqlProvider
Declaration
Func<ISqlBuilder> IDataContext.CreateSqlProvider { get; }
Returns
|
Improve this Doc
View Source
IDataContext.DataReaderType
Declaration
Type IDataContext.DataReaderType { get; }
Returns
|
Improve this Doc
View Source
IDataContext.GetQueryRunner(Query, Int32, Expression, Nullable<Object>[], Nullable<Object>[])
Declaration
IQueryRunner IDataContext.GetQueryRunner(Query query, int queryNumber, Expression expression, object? [] parameters, object? [] preambles)
Parameters
Type |
Name |
Description |
Query |
query |
|
System.Int32 |
queryNumber |
|
Expression |
expression |
|
System.Nullable<System.Object>[] |
parameters |
|
System.Nullable<System.Object>[] |
preambles |
|
Returns
|
Improve this Doc
View Source
IDataContext.GetReaderExpression(DbDataReader, Int32, Expression, Type)
Declaration
Expression IDataContext.GetReaderExpression(DbDataReader reader, int idx, Expression readerExpression, Type toType)
Parameters
Type |
Name |
Description |
DbDataReader |
reader |
|
System.Int32 |
idx |
|
Expression |
readerExpression |
|
Type |
toType |
|
Returns
Type |
Description |
Expression |
|
|
Improve this Doc
View Source
IDataContext.GetSqlOptimizer
Declaration
Func<DataOptions, ISqlOptimizer> IDataContext.GetSqlOptimizer { get; }
Returns
|
Improve this Doc
View Source
IDataContext.IsDBNullAllowed(DbDataReader, Int32)
Declaration
bool? IDataContext.IsDBNullAllowed(DbDataReader reader, int idx)
Parameters
Type |
Name |
Description |
DbDataReader |
reader |
|
System.Int32 |
idx |
|
Returns
Type |
Description |
System.Nullable<System.Boolean> |
|
|
Improve this Doc
View Source
IDataContext.SqlProviderFlags
Declaration
SqlProviderFlags IDataContext.SqlProviderFlags { get; }
Returns
|
Improve this Doc
View Source
IDataContext.SupportedTableOptions
Declaration
TableOptions IDataContext.SupportedTableOptions { get; }
Returns
|
Improve this Doc
View Source
IDataContext.UnwrapDataObjectInterceptor
Declaration
IUnwrapDataObjectInterceptor IDataContext.UnwrapDataObjectInterceptor { get; }
Returns
|
Improve this Doc
View Source
IInterceptable<ICommandInterceptor>.Interceptor
Declaration
ICommandInterceptor IInterceptable<ICommandInterceptor>.Interceptor { get; set; }
Returns
|
Improve this Doc
View Source
IInterceptable<IConnectionInterceptor>.Interceptor
Declaration
IConnectionInterceptor IInterceptable<IConnectionInterceptor>.Interceptor { get; set; }
Returns
|
Improve this Doc
View Source
IInterceptable<IDataContextInterceptor>.Interceptor
Declaration
IDataContextInterceptor IInterceptable<IDataContextInterceptor>.Interceptor { get; set; }
Returns
|
Improve this Doc
View Source
IInterceptable<IEntityServiceInterceptor>.Interceptor
Declaration
IEntityServiceInterceptor IInterceptable<IEntityServiceInterceptor>.Interceptor { get; set; }
Returns
|
Improve this Doc
View Source
IInterceptable<IUnwrapDataObjectInterceptor>.Interceptor
Declaration
IUnwrapDataObjectInterceptor IInterceptable<IUnwrapDataObjectInterceptor>.Interceptor { get; set; }
Returns
Implements
IDisposable
Extension Methods