Show / Hide Table of Contents

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

    .NET Standard 2.x

    Tracks a database table delete operation and an Action<ISaveContext> object. The data that will be deleted from the database table is cached in the TModel object. When ISqlModelMapper.SaveChanges method is called, a SQL DELETE statement 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 ISqlModelMapper TrackDelete<TModel>(TModel model, Action<ISaveContext> afterSaveAction);
    

    Type Parameters

    TModel

    The type of a model class.

    Parameters

    model TModel

    The TModel instance that corresponds to the database record.

    afterSaveAction System.Action<SnapObjects.Data.ISaveContext>

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

    Returns

    SnapObjects.Data.ISqlModelMapper

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

    Remarks

    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. See ISaveContext for more info.

    Examples

    The following code example demonstrates how to delete a student record and use an Action.

    using Appeon.ApiDoc.Models.School;
    using System;
    
    namespace Appeon.ApiDoc.ISqlModelMapperExamples
    {
        public class TrackDeleteExample
        {
            private SchoolContext _context;
    
            public TrackDeleteExample(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"
                };
    
                // Tracks the new student and saves changes to the database.
                var dbResult = mapper.TrackCreate(newStudent)
                                     .SaveChanges();
    
                Console.WriteLine("{0} record has been inserted into the database.",
                    dbResult.InsertedCount);
    
                var deletedCount = 0;
                mapper.TrackDelete(newStudent,
                    (saveContext) =>
                    {
                        // Gets the last DB result.
                        deletedCount = saveContext.LastDbResult.DeletedCount;
                    });
    
                mapper.SaveChanges();
    
                Console.WriteLine("{0} record has been deleted from the database.",
                    deletedCount);
    
                /* The code produces the following output:
                
                1 record has been inserted into the database.
                1 record has been deleted from the database.
                */
            }
        }
    }
    

    Example Refer To

    Model Class: Person

    Applies to

    .NET Standard

    2.x

    Back to top Generated by Appeon