Show / Hide Table of Contents

    ISqlModelMapper.Track<TModel>(IModelEntry<TModel> modelEntry, Action<ISaveContext> afterSaveAction) Method

    .NET Standard 2.x

    Tracks a database table operation (insert, update, or delete) and an Action<ISaveContext> object. The data state determines which type of operation (insert, update, or delete) to be performed. The data to be manipulated is cached in the TModel object. When ISqlModelMapper.SaveChanges method is called, a SQL statement (INSERT, UPDATE or DELETE) will be first generated using the data cached in the TModel object and the mapping information defined in TModel class, and then executed; and after that, the Action<ISaveContext> object will be called.

    Namespace: SnapObjects.Data

    Assembly: SnapObjects.Data.dll

    Syntax

    public TModel Track<TModel>(IModelEntry<TModel> modelEntry, Action<ISaveContext> afterSaveAction);
    

    Type Parameters

    TModel

    The type of a model class.

    Parameters

    modelEntry SnapObjects.Data.IModelEntry<TModel>

    An IModelEntry<TModel> object including data and state.

    afterSaveAction System.Action<SnapObjects.Data.ISaveContext>

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

    Returns

    TModel

    Returns a tracked TModel instance which includes the data from the IModelEntry<TModel> object.

    Remarks

    If you need to continue to modify the tracked data after calling this method, please do not modify the modelEntry object, but modify the model object returned by this method.

    The update to the database and the call to the specified Action<ISaveContext> object are in the same database transaction. The database transaction will not be committed until all of the tracked directives are performed.

    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 register a new student.

    It tracks the ModelEntry instance and an action for the student, then tracks the ModelEntry instance and an action for the student grade. When saving the changes, it starts a database transaction, inserts the student record, gets the new person ID generated by the database, sets the new person ID into the newStudentGrade object, inserts a student grade record, and commits the transaction at last.

    using SnapObjects.Data;
    using Appeon.ApiDoc.Models.School;
    using System;
    
    namespace Appeon.ApiDoc.ISqlModelMapperExamples
    {
        public class TrackExample
        {
            private SchoolContext _context;
    
            public TrackExample(SchoolContext dataContext)
            {
                // Sets the data context.
                _context = dataContext;
            }
    
            public void Example3(IModelEntry<Person> newStudent,
                IModelEntry<StudentGrade> newStudentGrade)
            {
                var mapper = _context.SqlModelMapper;
    
                // Tracks the Insert operation for the new student.
                mapper.Track(newStudent,
                            (saveContext) =>
                            {
                                // Gets the person ID generated by the database.
                                var newId = newStudent.GetCurrentValue("PersonID");
    
                                // Sets the student ID to the student grade record.
                                newStudentGrade.SetCurrentValue("StudentID", newId);
                            });
    
                // Tracks the Insert operation for the new student grade record.
                mapper.Track(newStudentGrade);
    
                // When saving the changes, it inserts the student record, gets the new 
                // person ID generated by the database, sets the new person ID to the 
                // newStudentGrade object, and inserts a student grade record. 
                var dbResult = mapper.SaveChanges();
    
                Console.WriteLine("{0} records have been inserted into the database.",
                    dbResult.InsertedCount);
    
                /* The code produces the following output:
                
                2 records have been inserted into the database.
                */
            }
        }
    }
    

    Example Refer To

    Model Classes: Person StudentGrade

    Applies to

    .NET Standard

    2.x

    Back to top Generated by Appeon