Show / Hide Table of Contents

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

    .NET Standard 2.x

    Tracks a database table insert operation and an Action<ISaveContext> object. The data that will be inserted into the database table is cached in the TModel object. When ISqlModelMapper.SaveChanges method is called, a SQL INSERT 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 TrackCreate<TModel>(TModel model, Action<ISaveContext> afterSaveAction);
    

    Type Parameters

    TModel

    The type of a model class.

    Parameters

    model TModel

    A model instance used for adding a record.

    saveAction 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

    The database table insert operation 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 track a new student and a callback Action.

    using Appeon.ApiDoc.Models.School;
    using System;
    
    namespace Appeon.ApiDoc.ISqlModelMapperExamples
    {
        public class TrackCreateExample
        {
            private SchoolContext _context;
    
            public TrackCreateExample(SchoolContext dataContext)
            {
                // Sets the data context.
                _context = dataContext;
            }
    
            public void Example2()
            {
                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 new student and a callback action.
                mapper.TrackCreate(newStudent,
                                   (saveContext) =>
                                    {
                                        // After inserting the student record, it will set 
                                        // the new person ID to the Student Grade record.
                                        newStudentGrade.StudentID = newStudent.PersonID;
                                    });
    
                // 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. 
                var dbResult = mapper.TrackCreate(newStudentGrade)
                                     .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