Show / Hide Table of Contents

    ISqlModelMapper.TrackUpdate<TModel>(TModel model, Action<ISaveContext> afterSaveAction) Method

    .NET Standard 2.x

    Tracks a database table update operation and an Action<ISaveContext> object. The data that will be updated to the database table is cached in the TModel object. When ISqlModelMapper.SaveChanges method is called, a SQL UPDATE statement will be first generated using the data cached in the TModel object and the mapping information defined in TModel class, and then executed. After that, the Action<ISaveContext> object will be called.

    Namespace: SnapObjects.Data

    Assembly: SnapObjects.Data.dll

    Syntax

      public ISqlModelMapper TrackUpdate<TModel>(TModel model, Action<ISaveContext> afterSaveAction);
    

    Type Parameters

    TModel

    The type of a model class.

    Parameters

    model TModel

    The TModel object that contains the data you want to update to database.

    afterSaveAction System.Action<SnapObjects.Data.ISaveContext>

    An Action<ISaveContext> object that needs to be tracked.

    Returns

    SnapObjects.Data.ISqlModelMapper

    Returns the current ISqlModelMapper object, which can be used for executing other methods.

    Remarks

    When instantiating the Action<ISaveContext> object, the generic parameter ISaveContext can be used for reading and writing the context which is used internally for saving data.

    Examples

    The following code example demonstrates how to use TrackUpdate<TModel>(TModel, Action<ISaveContext> ) method to track a database table update operation and an Action.

    using Appeon.ApiDoc.Models.School;
    using System;
    
    namespace Appeon.ApiDoc.ISqlModelMapperExamples
    {
        public class TrackUpdateExample
        {
            private SchoolContext _context;
    
            public TrackUpdateExample(SchoolContext dataContext)
            {
                // Sets the data context.
                _context = dataContext;
            }
    
            public void Example2()
            {
                var mapper = _context.SqlModelMapper;
    
                // Gets the Engineering Department record (DepartmentID = 1) from 
                // the database.
                var department = mapper.LoadByKey<Department>(1).FirstOrDefault();
    
                mapper.TrackUpdate(department,
                    (saveContext) =>
                    {
                        Console.WriteLine("AfterSaveAction executed!");
                        Console.WriteLine("AffectedCount:{0}",
                            saveContext.LastDbResult.AffectedCount);
                    });
    
                Console.WriteLine("The name is {0} (DepartmentID = 1).",
                   department.Name);
    
                department.Name = "New Name";
    
                mapper.SaveChanges();
    
                // Gets data from the database (DepartmentID = 1).
                department = mapper.LoadByKey<Department>(1).FirstOrDefault();
                Console.WriteLine("The name is \"{0}\" for the department when DepartmentID is 1.",
                   department.Name);
    
                /*The code example produces the following output:
                
                The name is Engineering (DepartmentID = 1).
                AfterSaveAction executed!
                AffectedCount:1
                The name is New Name (DepartmentID = 1).
                */
            }
        }
    }
    

    Example Refer To

    Model Class: Department

    Applies to

    .NET Standard

    2.x

    Back to top Generated by Appeon