DataContext Class
.NET Standard 2.x
Represents the entry to the database, which contains the database connection information, methods for manipulating database transactions, and other easy-to-use properties.
Namespace: SnapObjects.Data
Assembly: SnapObjects.Data.dll
Syntax
public abstract class DataContext : IDisposable;
Constructors
Name | Description |
---|---|
DataContext(IDataContextOptions dataContextOptions) | Initializes a new instance of the DataContext class by an IDataContextOptions object. |
DataContext(DbConnection dbConnection, IDataContextOptions dataContextOptions) | Initializes a new instance of the DataContext class by a DbConnnection object and an IDataContextOptions object. |
Properties
Name | Return Type | Description |
---|---|---|
ContextOptions | IDataContextOptions | Gets an IDataContextOptions object which contains options of the DataContext object. |
CurrentConnection | IAdoDbConnection | Gets an IAdoDbConnection object which can be used to get detailed information about the database connection, as well as open or close the database connection. |
CurrentTransaction | IAdoDbTransaction | Gets an IAdoDbTransaction object which can be used to get information about the database transaction, as well as commit or roll back the transaction. |
SqlModelMapper | ISqlModelMapper | Gets an ISqlModelMapper object created by the current DataContext . |
SqlExecutor | ISqlExecutor | Gets an ISqlExecutor object created by the current DataContext . |
Methods
Name | Return Type | Description |
---|---|---|
BeginTransaction() | IAdoDbTransaction | Starts a database transaction, using the isolation level specified by the ContextOptions . |
BeginTransaction(IsolationLevel isolationLevel) | IAdoDbTransaction | Starts a database transaction using the specified isolation level. |
BeginTransactionAsync() | Task<IAdoDbTransaction> | Asynchronously starts a database transaction, using the isolation level specified by the ContextOptions . |
BeginTransactionAsync(CancellationToken cancellationToken) | Task<IAdoDbTransaction> | Asynchronously starts a database transaction, using the isolation level specified by the ContextOptions . |
BeginTransactionAsync(IsolationLevel isolationLevel) | Task<IAdoDbTransaction> | Asynchronously starts a database transaction using the specified isolation level. |
BeginTransactionAsync(IsolationLevel isolationLevel, CancellationToken cancellationTokenl) | Task<IAdoDbTransaction> | Asynchronously starts a database transaction using the specified isolation level. |
Commit() | void | Commits the database transaction. |
Dispose() | void | Releases the database transaction. |
Rollback() | void | Rolls back the current database transaction from a pending state. |
UseTransaction(DbTransaction transaction) | IAdoDbTransaction | Uses a transaction as the current database transaction. |
Examples
The following code example demonstrates how to create a DataContextExample
class which can be used to connect a SQL server database.
using Microsoft.Data.SqlClient;
using SnapObjects.Data;
using SnapObjects.Data.SqlServer;
namespace Appeon.ApiDoc.DataContextExamples
{
public class DataContextExample : SqlServerDataContext
{
/// <summary>
/// The following code example uses the DataContextOptions parameter to create a DataContext class.
/// </summary>
public DataContextExample(IDataContextOptions<DataContextExample> options)
: base((SqlServerDataContextOptions)options)
{
}
/// <summary>
/// The following code example uses the SqlConnection parameter to create a DataContext class.
/// </summary>
public DataContextExample(SqlConnection conns)
: base(conns)
{
}
}
}