Show / Hide Table of Contents

    ISqlModelMapper.TrackCreate<TModel>(TModel model) Method

    .NET Standard 2.x

    Tracks a database table insert operation. 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.

    Namespace: SnapObjects.Data

    Assembly: SnapObjects.Data.dll

    Syntax

      public ISqlModelMapper TrackCreate<TModel>(TModel model);
    

    Type Parameters

    TModel

    The type of a model class.

    Parameters

    model TModel

    A TModel instance used for adding a record.

    Returns

    SnapObjects.Data.ISqlModelMapper

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

    Examples

    The following code example demonstrates how to register a new student.

    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 Example1()
            {
                var mapper = _context.SqlModelMapper;
    
                // Sets the properties of the 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);
    
                // Loads from database according to the person ID.
                var student = mapper.LoadByKey<Person>(newStudent.PersonID)
                                    .FirstOrDefault();
    
                Console.WriteLine();
                Console.WriteLine("The new student:");
                Console.WriteLine("Name is {0}.",
                    student.FirstName + " " + student.LastName);
                Console.WriteLine("Enrollment date is {0}.",
                    student.EnrollmentDate.Value.ToString("yyyy-MM-dd"));
    
                /* The code produces the following output:
                
                1 record has been inserted into the database.
    
                The new student:
                Name is Letitia Senior.
                Enrollment date is 2019 - 01 - 01.
                */
            }
        }
    }
    

    Example Refer To

    Model Class: Person

    Applies to

    .NET Standard

    2.x

    Back to top Generated by Appeon