Show / Hide Table of Contents

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

    .NET Standard 2.x

    Tracks an insert, update or delete operation to the master table and an Action<ISaveContext> object, when working with multiple models which are in the master-detail relationship. The data state determines which type of operation (insert, update or delete) will 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 IDetailTracker<TModel> TrackMaster<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 which includes the data and state of the master model.

    afterSaveAction System.Action<ISaveContext>

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

    Returns

    SnapObjects.Data.IDetailTracker<TModel>

    An interface that can be used to track the detail model.

    Remarks

    The ModelEmbedded attribute must be applied in TModel to create the master-detail relationship. Refer to ModelEmbedded attribute for more info.

    The delete operation to the database record 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 update a single table.

    using SnapObjects.Data;
    using Appeon.ApiDoc.Models.School;
    using System;
    using System.Collections.Generic;
    
    namespace Appeon.ApiDoc.ISqlModelMapperExamples
    {
        public class TrackMasterExample
        {
            private readonly SchoolContext _context;
    
            public TrackMasterExample(SchoolContext dataContext)
            {
                // Sets the data context.
                _context = dataContext;
            }
    
            public void Example2(IModelEntry<CourseAndOnlineInfo> course)
            {
                // Saves the changes.
                var mapper = _context.SqlModelMapper;
    
                var dbResult = mapper.TrackMaster(course)
                                     .SaveChanges();
    
                // Shows the saving result.
                Console.WriteLine("{0} records have been inserted into the database.",
                                  dbResult.AffectedCount);
    
                /* The code example produces the following output:
    
                1 records have been inserted into the database.
                */
            }
        }
    }
    

    Example Refer To

    Model Classes: CourseAndOnlineInfo OnlineCourse

    Applies to

    .NET Standard

    2.x

    Back to top Generated by Appeon