Show / Hide Table of Contents

    ISqlModelMapper.TrackDeleteByKey<TModel>(params object[] parameters) Method

    .NET Standard 2.x

    Tracks a database table delete operation by the primary key. A SQL DELETE statement will be generated using the primary key value(s) and the mapping information defined in TModel. When ISqlModelMapper.SaveChanges method is called, the SQL DELETE statement will be executed.

    Namespace: SnapObjects.Data

    Assembly: SnapObjects.Data.dll

    Syntax

      public ISqlModelMapper TrackDeleteByKey<TModel>(params object[] parameters);
    

    Type Parameters

    TModel

    The type of a model class.

    Parameters

    parameters System.Object[]

    (Optional) One or more primary key values that you want to use as arguments in the SQL DELETE statement.

    Returns

    SnapObjects.Data.ISqlModelMapper

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

    Remarks

    Cascade Delete

    If the record in the master table and its associated records in the detail table must be deleted at the same time, you can define the cascade deletion rule for the master and the detail tables in TModel. Refer to the ModelEmbedded attribute for more.

    Examples

    The following code example demonstrates how to delete a student record by the primary key.

    using Appeon.ApiDoc.Models.School;
    using System;
    
    namespace Appeon.ApiDoc.ISqlModelMapperExamples
    {
        public class TrackDeleteByKeyExample
        {
            private SchoolContext _context;
    
            public TrackDeleteByKeyExample(SchoolContext dataContext)
            {
                // Sets the data context.
                _context = dataContext;
            }
    
            public void Example()
            {
                var mapper = _context.SqlModelMapper;
    
                // Creates a new student.
                var newStudent = new Person()
                {
                    LastName = "Senior",
                    FirstName = "Letitia",
                    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);
    
                // Deletes the student from the database according to the person ID.
                dbResult = mapper.TrackDeleteByKey<Person>(newStudent.PersonID)
                                .SaveChanges();
    
                Console.WriteLine("{0} record has been deleted from the database.",
                    dbResult.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