Show / Hide Table of Contents

    IAdoDbTransaction Interface

    .NET Standard 2.x

    Represents the database transaction.

    Namespace: SnapObjects.Data

    Assembly: SnapObjects.Data.dll

    Syntax

       public interface IAdoDbTransaction : IDisposable;
    

    Property

    Name Returns Type Description
    DbTransaction DbTransaction Gets the internal DbTransaction object.
    TransactionId Guid Gets the GUID of the current IAdoDbTransaction object.

    Method

    Name Returns Type Description
    Commit() void Commits the database transaction.
    CommitAsync() Task Asynchronously commits the database transaction.
    CommitAsync(CancellationToken cancellationToken) Task Asynchronously commits the database transaction.
    Rollback() void Rolls back the database transaction.
    RollbackAsync() Task Asynchronously rolls back the database transaction.
    RollbackAsync(CancellationToken cancellationToken) Task Asynchronously rolls back the database transaction.

    Examples

    Example 1

    The following code example demonstrates how to use the methods and properties of the IAdoDbTransaction object.

    using Appeon.ApiDoc.Models.School;
    using SnapObjects.Data;
    using System;
    
    namespace Appeon.ApiDoc.IAdoDbTransactionExamples
    {
        public class IAdoDbTransactionExample
        {
            private readonly SchoolContext _context;
    
            public IAdoDbTransactionExample(SchoolContext context)
            {
                _context = context;
            }
    
            public void Example1()
            {
                IAdoDbTransaction transobject;
    
                // Starts a transaction and assigns the current 
                // transaction to the IAdoDbTransaction object
                transobject = _context.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);
                }
            }
        }
    }
    

    Example 2

    The following code example demonstrates how to use the async methods and properties of the IAdoDbTransaction object.

            public async void Example2()
            {
                IAdoDbTransaction transobject;
    
                // Starts an asynchronous transaction and assigns the current 
                // transaction to the IAdoDbTransaction object
                transobject = await _context.BeginTransactionAsync();
    
                // 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);
                    await mapper.SaveChangesAsync();
    
                    // Commits changes to the database
                    await transobject.CommitAsync();
    
                    // 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
                    await transobject.RollbackAsync();
    
                    // Shows the error message and the transaction ID
                    Console.WriteLine("{0}, TransactionId = {1}", e.Message, transId);
                }
            }
    

    Example(s) Refer To

    Model Class: Person

    Back to top Generated by Appeon