Show / Hide Table of Contents

    ISqlModelMapper.TrackCreateRange<TModel>(IEnumerable<TModel> models) Method

    .NET Standard 2.x

    Tracks one or more database table insert operations. The data that will be inserted into the database table is cached in the TModel object. When ISqlModelMapper.SaveChanges method is called, one or more SQL INSERT statements 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 TrackCreateRange<TModel>(IEnumerable<TModel> models);
    

    Type Parameters

    TModel

    The type of a model class.

    Parameters

    models System.Collections.Generic.IEnumerable<TModel>

    A collection of TModel type used for adding one or multiple records.

    Returns

    SnapObjects.Data.ISqlModelMapper

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

    Remarks

    After calling multiple track methods of SqlModelMapper, the order of tracks determines the order on which operations to the database will be performed by the SaveChanges method.

    Examples

    The following code example demonstrates how to register a list of students.

    using Appeon.ApiDoc.Models.School;
    using System;
    using System.Collections.Generic;
    
    namespace Appeon.ApiDoc.ISqlModelMapperExamples
    {
        public class TrackCreateRangeExample
        {
            private SchoolContext _context;
    
            public TrackCreateRangeExample(SchoolContext dataContext)
            {
                // Sets the data context.
                _context = dataContext;
            }
    
            public void Example()
            {
                var mapper = _context.SqlModelMapper;
    
                // Sets the properties of the new students.
                var newStudentA = new Person()
                {
                    LastName = "Ruskin",
                    FirstName = "Lionel",
                    EnrollmentDate = new DateTime(2019, 1, 1),
                    Discriminator = "Student"
                };
    
                var newStudentB = new Person()
                {
                    LastName = "Effie",
                    FirstName = "Lilith",
                    EnrollmentDate = new DateTime(2019, 1, 1),
                    Discriminator = "Student"
                };
    
                var students = new List<Person>() { newStudentA, newStudentB };
    
                // Tracks the new students and saves changes to the database.
                var dbResult = mapper.TrackCreateRange(students)
                                     .SaveChanges();
    
                Console.WriteLine("{0} records have been inserted into the database.",
                    dbResult.InsertedCount);
    
                // Loads from database according to the person ID of student A.
                var student = mapper.LoadByKey<Person>(students[0].PersonID)
                                    .FirstOrDefault();
    
                Console.WriteLine();
                Console.WriteLine("Student A:");
                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:
                
                2 records have been inserted into the database.
    
    
                Student A:
                Name is Lionel Ruskin.
                Enrollment date is 2019 - 01 - 01.
                */
            }
        }
    }
    

    Example Refer To

    Model Class: Person

    Applies to

    .NET Standard

    2.x

    Back to top Generated by Appeon