Show / Hide Table of Contents

    IDbResult Interface

    .NET Standard 2.x

    Provides a series of properties for getting the result of saving the data changes to the database.

    Namespace: SnapObjects.Data

    Assembly: SnapObjects.Data.dll

    Syntax

      public interface IDbResult;
    

    Properties

    Name Return Type Description
    AffectedCount int Gets the amount of rows that are affected when saving the data changes to the database.
    Cancelled bool Gets whether the operation is cancelled when saving the data changes to the database.
    DeletedCount int Gets the amount of rows that are deleted when saving the data changes to the database.
    InsertedCount int Gets the amount of rows that are inserted when saving the data changes to the database.
    ModifiedCount int Gets the amount of rows that are modified when saving the data changes to the database.

    Remarks

    The IDbResult object can be used to get more details about the result after calling the ISqlModelMapper.SaveChanges method, or accessing the ISaveContext.LastDbResult property.

    Examples

    Example 1

    The following code example demonstrates how to use ISqlModelMapper to create a new department and get the last DB result.

    using SnapObjects.Data;
    using Appeon.ApiDoc.Models.School;
    using System;
    using System.Collections.Generic;
    
    namespace Appeon.ApiDoc.IDbResultExamples
    {
        public class IDbResultExample
        {
            private SchoolContext _context;
    
            public IDbResultExample(SchoolContext dataContext)
            {
                // Sets the data context.
                _context = dataContext;
            }
    
            public void Example1()
            {
                var newDept = new Department()
                {
                    DepartmentID = 10,
                    Name = "Chemistry",
                    Budget = 190000,
                    StartDate = new DateTime(2019, 1, 1)
                };
    
                var dbResult = _context.SqlModelMapper.TrackCreate(newDept)
                                                    .SaveChanges();
    
                // Outputs the
                Console.WriteLine("The count of the affected records is {0}.",
                    dbResult.AffectedCount);
                Console.WriteLine("The count of the inseted records is {0}.",
                    dbResult.InsertedCount);
    
                /* The code produces the following output:
                
                The count of the affected records is 1.
                The count of the inseted records is 1.
                */
            }
        }
    }
    

    Example 2

    The following code example demonstrates how to use ISqlModelMapper and ISaveContext to get the last DB result.

            public void Example2()
            {
                // Supposes there are two Department objects that needs to be deleted.
                var literatureDept = new Department()
                {
                    DepartmentID = 11,
                    Name = "Literature",
                };
                var ManagementDept = new Department()
                {
                    DepartmentID = 12,
                    Name = "Management",
                };
    
                var depts = new List<Department>() { literatureDept, ManagementDept };
    
    
                // Deletes the two departments.
                int affectedCount = 0;
                var dbResult = _context.SqlModelMapper.TrackDeleteRange(depts)
                    .Track((saveContext) =>
                    {
                        // Gets the count of the affected records.
                        affectedCount = saveContext.LastDbResult.AffectedCount;
    
                        // If at least one record has not been deleted, you will think 
                        // that this is not correct and want to cancel the save operator.
                        if (affectedCount != depts.Count)
                        {
                            // The CancelSave method will roll back the database transaction.
                            saveContext.CancelSave();
                        }
                    })
                    .SaveChanges();
    
                Console.WriteLine("The count of the affected records is {0}.",
                     affectedCount);
                Console.WriteLine("The count of the real affected records is {0}.",
                    dbResult.AffectedCount);
                Console.WriteLine("The save operation was cancelled: {0}.",
                    dbResult.Cancelled);
    
                /* The code produces the following output:
                
                The count of the affected records is 0.
                The count of the real affected records is 0.
                The save operation was cancelled: True.
                */
            }
    

    Example(s) Refer To

    Model Class: Department

    Back to top Generated by Appeon