IDetailTracker.TrackGrandDetail<TDetailModel, TGrandDetailModel>(Expression<Func<TModel, object>> propertyExpr, Expression<Func<TDetailModel, object>> detailPropertyExpr, IModelEntry<TGrandDetailModel> grandModelEntry) Method
.NET Standard 2.x
Tracks an insert, update or delete operation on the table which the granddetail model is mapped to when working with the master-detail-granddetail models where the ModelEmbedded attribute is applied in a property of the master model and a property of the detail model. The data state determines which type of operation to be performed. The data to be manipulated is cached in a TGrandDetailModel
object.
When ISqlModelMapper.SaveChanges is called, a SQL statement (INSERT, UPDATE, or DELETE) will be generated using the data cached in the TGrandDetailModel
object and the mapping information defined in TGrandDetailModel
, and then the SQL statement will be executed.
Namespace: SnapObjects.Data
Assembly: SnapObjects.Data.dll
Syntax
IDetailTracker<TModel> TrackGrandDetail<TDetailModel, TGrandDetailModel>(Expression<Func<TModel, object>> propertyExpr, IModelEntry<TDetailModel> modelEntry, Expression<Func<TDetailModel, object>> detailPropertyExpr, IModelEntry<TGrandDetailModel> grandModelEntry);
Type Parameters
TDetailModel
The type of the detail model class.
TGrandDetailModel
The type of the granddetail model class.
Parameters
propertyExpr
System.Linq.Expressions.Expression<Func<TModel, object>>
A property of the master model (TModel
) where the ModelEmbedded attribute is applied.
The type of this property is TDetailModel
.
This property is specified by an expression.
detailPropertyExpr
System.Linq.Expressions.Expression<Func<TDetailModel, object>>
A property of the detail model where the ModelEmbedded attribute is applied.
The type of this property is TGrandDetailModel
.
This property is specified by an expression.
grandModelEntry
SnapObjects.Data.IModelEntry<TGrandDetailModel>
An IModelEntry<TGrandDetailModel>
object that contains the data and data state and which will be tracked. The data corresponds to the property specified by detailPropertyExpr
.
Returns
SnapObjects.Data.IDetailTracker<TModel>
Returns the current IDetailTracker<TModel>
object.
Remarks
The ModelEmbedded attribute must be applied in TModel
and TDetailModel
to create the master-detail-granddetail relationship between TModel
, TDetailModel
, and TGrandDetailModel
.
The ISqlModelMapper.TrackMaster method can track the master model and return the IDetailTracker<TModel>
object. The IDetailTracker.TrackDetail method can be used to track the detail model. Then the IDetailTracker.TrackGrandDetail
method can be used to track the granddetail model.
One-to-one-to-one
The type of the property specified by the propertyExpr
parameter is TDetailModel
(which represents one row in the detail database table). The type of the property specified by the detailPropertyExpr
parameter is TGrandDetailModel
(which represents one row in the granddetail database table). Therefore, the IDetailTracker.TrackDetailAndGrandDetail
method applies to one-to-one-to-one table relationship (one master table record to one detail table record to one granddetail table record).
Examples
The following code example demonstrates how to save the master table, the detail table, and the grand-detail table synchronously; all of them have only one affected record.
using SnapObjects.Data;
using Appeon.ApiDoc.Models.School;
using System;
namespace Appeon.ApiDoc.IDetailTrackerExamples
{
public class TrackGrandDetailExample
{
private readonly SchoolContext _context;
public TrackGrandDetailExample(SchoolContext dataContext)
{
// Sets the data context.
_context = dataContext;
}
public void Example(IModelEntry<DepartmentInfo> department,
IModelEntry<CourseInfo> course,
IModelEntry<CourseStudentInfo> courseStudent)
{
// Saves the master table, the detail table, and the grand-detail table.
var mapper = _context.SqlModelMapper;
var master = mapper.TrackMaster(department)
.TrackDetail(m => m.Courses, course)
.TrackGrandDetail<CourseInfo, CourseStudentInfo>(
m => m.Courses,
d => d.Students,
courseStudent)
.MasterModel;
var dbResult = mapper.SaveChanges();
// Shows the saving result.
Console.WriteLine("{0} records have been inserted into the database.",
dbResult.AffectedCount);
/* This code example produces the following output:
3 records have been inserted into the database.
*/
}
}
}
Example Refer To
Model Classes: CourseInfo CourseStudentInfo DepartmentInfo
Applies to
.NET Standard
2.x