ISqlModelMapper.TrackUpdate<TModel>(TModel originalModel, TModel modifiedModel, Action<ISaveContext> afterSaveAction) Method
.NET Standard 2.x
Tracks a database table update operation and an Action<ISaveContext>
object. 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. After that, the Action<ISaveContext>
object will be called.
Namespace: SnapObjects.Data
Assembly: SnapObjects.Data.dll
Syntax
public ISqlModelMapper TrackUpdate<TModel>(TModel originalModel, TModel modifiedModel, Action<ISaveContext> afterSaveAction);
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.
afterSaveAction
System.Action<SnapObjects.Data.ISaveContext>
An Action<ISaveContext>
object that needs to be tracked.
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, Action<ISaveContext>)
method to track a database table update operation and an Action.
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 Example4()
{
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,
(saveContext) =>
{
// It will callback the Action to output the affectedCount.
Console.WriteLine("AfterSaveAction executed!");
Console.WriteLine("AffectedCount:{0}",
saveContext.LastDbResult.AffectedCount);
});
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).
AfterSaveAction executed!
AffectedCount:1
The name is New Department (After SaveChanges).
*/
}
}
}
Example Refer To
Model Class: Department
Applies to
.NET Standard
2.x