ISqlModelMapper.TrackUpdate<TModel>(TModel originalModel, TModel modifiedModel) Method
.NET Standard 2.x
Tracks a database table update operation. The data that will be updated to the database table is cached in the modifiedModel
and the original data is cached in the originalModel
. When ISqlModelMapper.SaveChanges method is called, a SQL UPDATE statement will be first generated using the data cached in the modifiedModel
, the data cached in the originalModel
, and the mapping information defined in TModel
class, and then executed.
Namespace: SnapObjects.Data
Assembly: SnapObjects.Data.dll
Syntax
public ISqlModelMapper TrackUpdate<TModel>(TModel originalModel, TModel modifiedModel);
Type Parameters
TModel
The type of a model class.
Parameters
originalModel
TModel
The TModel
object that contains the original data you want to update.
modifiedModel
TModel
The TModel
object that contains the modified data you want to update to database.
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 use TrackUpdate<TModel>(TModel, TModel)
method to track a database table update operation.
using Appeon.ApiDoc.Models.School;
using System;
namespace Appeon.ApiDoc.ISqlModelMapperExamples
{
public class TrackUpdateExample
{
private SchoolContext _context;
public TrackUpdateExample(SchoolContext dataContext)
{
// Sets the data context.
_context = dataContext;
}
public void Example3()
{
var mapper = _context.SqlModelMapper;
// Gets the Engineering Department record (DepartmentID = 1) from
// the database.
var originalModel = mapper.LoadByKey<Department>(1).FirstOrDefault();
Console.WriteLine("The department name is {0} (Original).",
originalModel.Name);
// These is another model contains the data that contains the modified
// data you want to update to database.
var modifiedModel = new Department
{
DepartmentID = 1,
Name = "New Department",
Budget = 350000.00m,
StartDate = new DateTime(2007, 9, 1),
Administrator = 2
};
Console.WriteLine("The department name is {0} (Modified).",
modifiedModel.Name);
// The data that will be updated to the database table is cached in the
// modifiedModel and the original data in the originalModel.
mapper.TrackUpdate(originalModel, modifiedModel);
mapper.SaveChanges();
// Gets data from the database.
// DepartmentID = 1
var department = mapper.LoadByKey<Department>(1).FirstOrDefault();
Console.WriteLine("The name is {0} (After SaveChanges).",
department.Name);
/*The code example produces the following output:
The department name is Engineering (Original).
The department name is New Department (Modified).
The name is New Department (After SaveChanges).
*/
}
}
}
Example Refer To
Model Class: Department
Applies to
.NET Standard
2.x