Show / Hide Table of Contents

    IAdoDbTransactionManager Interface

    .NET Standard 2.x

    Provides methods to manage database transactions.

    Namespace: SnapObjects.Data

    Assembly: SnapObjects.Data.dll

    Syntax

       public interface IAdoDbTransactionManager;
    

    Method

    Name Returns Type Description
    BeginTransaction(string name) IAdoDbTransaction Starts a database transaction with the unspecified isolation level (IsolationLevel.Unspecified).
    BeginTransaction(IsolationLevel isolationLevel) IAdoDbTransaction Starts a database transaction with the specified isolation level.
    UseTransaction(string name, object value) IAdoDbTransaction Uses a specified database transaction when no transaction is already started.

    Examples

    Example 1

    The following code example demonstrates how to use the methods of the IAdoDbTransactionManager object.

    using Appeon.ApiDoc.Models.School;
    using SnapObjects.Data;
    using System;
    using System.Data;
    
    namespace Appeon.ApiDoc.IAdoDbTransactionManagerExamples
    {
        public class IAdoDbTransactionManagerExample
        {
            private readonly SchoolContext _context;
            
            // Declares an IAdoDbTransactionManager object
            private IAdoDbTransactionManager managerTrans;
            
            public IAdoDbTransactionManagerExample(SchoolContext context)
            {
                _context = context;
    
                // Instantiates an IAdoDbTransactionManager object
                managerTrans = _context.CurrentConnection;
            }
    
            public void Example1()
            {
                IAdoDbTransaction transobject;
    
                // Starts a transaction and assigns the current 
                // transaction to the IAdoDbTransactionManager object
                transobject = managerTrans.BeginTransaction();
    
                // Gets the transaction ID and DbTransaction
                var transId = transobject.TransactionId;
                var dbtrans = transobject.DbTransaction;
    
                // Performs data saving operation in the current transaction
                var mapper = _context.SqlModelMapper;
    
                var person = new Person()
                {
                    LastName = "Senior",
                    FirstName = "Letitia",
                    EnrollmentDate = new DateTime(2019, 1, 1),
                    Discriminator = "Student"
                };
    
                try
                {
                    mapper.TrackCreate(person);
                    mapper.SaveChanges();
    
                    // Commits changes to the database
                    transobject.Commit();
    
                    // Shows a success message and the transaction ID 
                    // if data is saved successfully
                    Console.WriteLine("Save Data Success, TransactionId = {0}", transId);
                }
                catch (Exception e)
                {
                    // Rolls back the transaction if failed to save data
                    transobject.Rollback();
    
                    // Shows the error message and the transaction ID
                    Console.WriteLine("{0}, TransactionId = {1}", e.Message, transId);
    
                    // Resets DbTransactionManager
                    managerTrans.UseTransaction(dbtrans);
                }
    
                /* The code produces the following output:
                
                Save Data Success, TransactionId = 56ece99e-0498-4865-8103-9778adae7553
                */
            }
        }
    }
    

    Example 2

    The following code example demonstrates how to use the methods of the IAdoDbTransactionManager object with the specified isolation level.

            public void Example2()
            {
                IAdoDbTransaction transobject;
    
                // Starts a transaction, assigns the current transaction to the
                // IAdoDbTransactionManager object, and specifies the isolation level.
                transobject = managerTrans.BeginTransaction(IsolationLevel.ReadCommitted);
    
                // Gets the transaction ID and DbTransaction
                var transId = transobject.TransactionId;
                var dbtrans = transobject.DbTransaction;
    
                // Performs data saving operation in the current transaction
                var mapper = _context.SqlModelMapper;
    
                var person = new Person()
                {
                    LastName = "Senior",
                    FirstName = "Letitia",
                    EnrollmentDate = new DateTime(2019, 1, 1),
                    Discriminator = "Student"
                };
    
                try
                {
                    mapper.TrackCreate(person);
                    mapper.SaveChanges();
    
                    // Commits changes to the database
                    transobject.Commit();
    
                    // Shows a success message and the transaction ID 
                    // if data is saved successfully
                    Console.WriteLine("Save Data Success, TransactionId = {0}", transId);
                }
                catch (Exception e)
                {
                    // Rolls back the transaction if failed to save data
                    transobject.Rollback();
    
                    // Shows the error message and the transaction ID
                    Console.WriteLine("{0}, TransactionId = {1}", e.Message, transId);
    
                    // Resets DbTransactionManager
                    managerTrans.UseTransaction(dbtrans);
                }
    
                /* The code produces the following output:
                
                Save Data Success, TransactionId = 56ece99e-0498-4865-8103-9778adae7553
                */
            }
    

    Example(s) Refer To

    Model Class: Person

    Back to top Generated by Appeon