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.
    Rollback() void Rolls back the database transaction.

    Examples

    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 Example()
            {
                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 Refer To

    Model Class: Person

    Back to top Generated by Appeon