ISqlModelMapper.Track<TModel>(IModelEntry<TModel> modelEntry) Method
.NET Standard 2.x
Tracks a database table operation (insert, update, or delete). The data state determines which type of operation (insert, update, or delete) to be performed. The data to be manipulated is cached in the TModel
object. When ISqlModelMapper.SaveChanges method is called, a SQL statement (INSERT, UPDATE or DELETE) 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 TModel Track<TModel>(IModelEntry<TModel> modelEntry);
Type Parameters
TModel
The type of a model class.
Parameters
modelEntry
SnapObjects.Data.IModelEntry<TModel>
An IModelEntry<TModel>
object including data and state.
Returns
TModel
Returns a tracked model object which includes the data from the IModelEntry<TModel>
object.
Remarks
If you need to continue to modify the tracked data after calling this method, please do not modify the modelEntry
object, but modify the model object returned by this method.
Examples
The following code example demonstrates how to register a new student.
It tracks the Insert operation for the new student because the ModelState property of IModelEntry is NewModified.
using SnapObjects.Data;
using Appeon.ApiDoc.Models.School;
using System;
namespace Appeon.ApiDoc.ISqlModelMapperExamples
{
public class TrackExample
{
private SchoolContext _context;
public TrackExample(SchoolContext dataContext)
{
// Sets the data context.
_context = dataContext;
}
public void Example2(IModelEntry<Person> newStudent)
{
var mapper = _context.SqlModelMapper;
Console.WriteLine("The state of the student record is {0}.",
newStudent.ModelState.ToString());
// Tracks the Insert operation for the new student.
mapper.Track(newStudent);
// Saves to the database.
var dbResult = mapper.SaveChanges();
Console.WriteLine("{0} student record has been inserted into the " +
"database.",
dbResult.InsertedCount);
/* The code produces the following output:
The state of the student record is NewModified.
1 student record has been inserted into the database.
*/
}
}
}
Example Refer To
Model Class: Person
Applies to
.NET Standard
2.x