ISqlModelMapper.TrackDeleteRange<TModel>(IEnumerable<TModel> models) Method
.NET Standard 2.x
Tracks one or more database table delete operations. The data that will be deleted from the database table is cached in the TModel
object. When ISqlModelMapper.SaveChanges method is called, one or more SQL DELETE 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 TrackDeleteRange<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 deleting one or multiple records.
Returns
SnapObjects.Data.ISqlModelMapper
Returns an ISqlModelMapper
object, which can be used for executing other methods.
Examples
The following code example demonstrates how to delete a list of students.
using Appeon.ApiDoc.Models.School;
using System;
using System.Collections.Generic;
namespace Appeon.ApiDoc.ISqlModelMapperExamples
{
public class TrackDeleteRangeExample
{
private SchoolContext _context;
public TrackDeleteRangeExample(SchoolContext dataContext)
{
// Sets the data context.
_context = dataContext;
}
public void Example()
{
var mapper = _context.SqlModelMapper;
// Creates two 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);
// Deletes these students from the database.
dbResult = mapper.TrackDeleteRange(students).SaveChanges();
Console.WriteLine("{0} records have been deleted from the database.",
dbResult.DeletedCount);
/* The code produces the following output:
2 records have been inserted into the database.
2 records have been deleted from the database.
*/
}
}
}
Example Refer To
Model Class: Person
Applies to
.NET Standard
2.x