Show / Hide Table of Contents

    ISqlModelMapper.Track(Action<ISaveContext> saveAction) Method

    .NET Standard 2.x

    Tracks an Action<ISaveContext> object. The Action will be called when ISqlModelMapper.SaveChanges method is called.

    Namespace: SnapObjects.Data

    Assembly: SnapObjects.Data.dll

    Syntax

      public ISqlModelMapper Track(Action<ISaveContext> saveAction);
    

    Parameters

    saveAction System.Action<SnapObjects.Data.ISaveContext>

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

    Please avoid calling any tracking method of the current ISqlModelMapper object in the script of the Action.

    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.

    After calling multiple track methods of the ISqlModelMapper object, the order of tracks determines the order of the actual operations to the database.

    Examples

    The following code example demonstrates how to register a new student and select the Physics course for this student.

    It tracks the Insert operation for the new student, then tracks an Action to set the new person ID that is generated by the database to the Student Grade record. 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 then inserts a student grade record.

    You can track an action to get some values generated by the database, and set values to the other tracked models in the same transaction, or load data from the database, or cancel the Save operation and roll back the transaction by Save Context.

    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 Example1()
            {
                var mapper = _context.SqlModelMapper;
    
                // Sets the properties of the new student.
                var newStudent = new Person()
                {
                    LastName = "Sherwood",
                    FirstName = "Lena",
                    EnrollmentDate = new DateTime(2019, 1, 1),
                    Discriminator = "Student"
                };
    
                // Selects the Physics course (CourseID = 1061) for the new student.
                var newStudentGrade = new StudentGrade()
                {
                    CourseID = 1061
                };
    
                // Tracks the Insert operation for the new student.
                mapper.TrackCreate(newStudent);
    
                // Tracks a callback action to set the new person ID to the Student 
                // Grade record.
                mapper.Track((saveContext) =>
                            {
                                Console.WriteLine("{0} student record has been "
                                                    + "inserted into the database.",
                                                    saveContext.LastDbResult
                                                                .InsertedCount);
    
                                newStudentGrade.StudentID = newStudent.PersonID;
    
                                // Adds the value to Save Context.
                                saveContext.Add("newPersonId", newStudent.PersonID);
                            });
    
                // Tracks the Insert operation for the new student grade record.
                mapper.TrackCreate(newStudentGrade);
    
                // Tracks an action to show the person ID generated by database.
                mapper.Track((saveContext) =>
                {
                    int newPersonID = (int)saveContext.Get("newPersonId");
                    Console.WriteLine("The person ID generated by database is: {0}.",
                        newPersonID);
    
                    // Loads the newly inserted student record in the transaction.
                    var student = mapper.LoadByKey<Person>(newPersonID)
                                        .FirstOrDefault();
                    Console.WriteLine("Name: {0} {1}",
                      student.FirstName, student.LastName);
                });
    
                // When saving the changes, it executes the tracked SQL operations and 
                // actions one by one in the same transaction and commit them at last.
                var dbResult = mapper.SaveChanges();
    
                Console.WriteLine("{0} records have been inserted into the database.",
                    dbResult.InsertedCount);
    
                /* The code produces the following output:
                
                1 student record has been inserted into the database.
                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