DataContext Class
.NET Standard 2.x |  Current Version (1.0.1) 
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;
Constructor
| Name | Description | 
|---|---|
| DataContext(IDataContextOptions dataContextOptions) | Initializes a new instance of the DataContextclass by anIDataContextOptionsobject. | 
| DataContext(DbConnection dbConnection, IDataContextOptions dataContextOptions) | Initializes a new instance of the DataContextclass by aDbConnnectionobject and anIDataContextOptionsobject. | 
Property
| Name | Return Type | Description | 
|---|---|---|
| ContextOptions | IDataContextOptions | Gets an IDataContextOptionsobject which contains options of theDataContextobject. | 
| CurrentConnection | IAdoDbConnection | Gets an IAdoDbConnectionobject 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 IAdoDbTransactionobject which can be used to get information about the database transaction, as well as commit or roll back the transaction. | 
| SqlModelMapper | ISqlModelMapper | Gets an ISqlModelMapperobject created by the currentDataContext. | 
| SqlExecutor | ISqlExecutor | Gets an ISqlExecutorobject created by the currentDataContext. | 
Method
| 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. | 
| Commit() | void | Commits 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 SnapObjects.Data;
using SnapObjects.Data.SqlServer;
using System.Data.SqlClient;
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)
        {
        }
    }
}