ISqlModelMapper.TrackMaster<TModel>(IModelEntry<TModel> modelEntry) Method
.NET Standard 2.x
Tracks an insert, update or delete operation to the master table, when working with multiple models which are in the master-detail relationship. The data state determines which type of operation (insert, update or delete) will 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 IDetailTracker<TModel> TrackMaster<TModel>(IModelEntry<TModel> modelEntry);
Type Parameters
TModel
The type of a model class.
Parameters
modelEntry
SnapObjects.Data.IModelEntry<TModel>
A ModelEntry<TModel>
object which includes the data and state of the master model.
Returns
SnapObjects.Data.IDetailTracker<TModel>
An IDetailTracker<TModel>
object that can be used to track the detail model.
Remarks
The ModelEmbedded attribute must be applied in TModel
to create the master-detail relationship. Refer to the ModelEmbedded attribute for more info.
Examples
The following code example demonstrates how to set the value of the primary key for the detail table and save changes.
using SnapObjects.Data;
using Appeon.ApiDoc.Models.School;
using System;
using System.Collections.Generic;
namespace Appeon.ApiDoc.ISqlModelMapperExamples
{
public class TrackMasterExample
{
private readonly SchoolContext _context;
public TrackMasterExample(SchoolContext dataContext)
{
// Sets the data context.
_context = dataContext;
}
public void Example1(IModelEntry<CourseAndOnlineInfo> course,
IModelEntry<OnlineCourse> onlineCourse)
{
// Sets the value of CourseID of the master table to the CourseID of the detail table.
// Saves the changes.
var mapper = _context.SqlModelMapper;
var master =
mapper.TrackMaster(course,
(saveAction) =>
{
onlineCourse.SetCurrentValue("CourseID",
course.GetCurrentValue("CourseID"));
})
.TrackDetail(m => m.OnlineCourse, onlineCourse)
.MasterModel;
var dbResult = mapper.SaveChanges();
// Shows the saving result.
Console.WriteLine("{0} records have been inserted into the database.",
dbResult.AffectedCount);
/* The code example produces the following output:
2 records have been inserted into the database.
*/
}
}
}
Example Refer To
Model Classes: CourseAndOnlineInfo OnlineCourse
Applies to
.NET Standard
2.x