IDetailTracker.TrackDetails<TDetailModel>(Expression<Func<TModel, object>> propertyExpr, IEnumerable<IModelEntry<TDetailModel>> modelEntry) Method
.NET Standard 2.x
Tracks one or more insert, update or delete operations on the table which the detail model is mapped to, when working with the master-detail models (where the ModelEmbedded attribute is applied in a property of the master model). The data state determines which type of operation to be performed. The data to be manipulated is cached in a sequence of TDetailModel
objects.
When ISqlModelMapper.SaveChanges is called, one or more SQL statements (INSERT, UPDATE, or DELETE) will be first generated using the data cached in TDetailModel
objects and the mapping information defined in TDetailModel
, and then executed.
Namespace: SnapObjects.Data
Assembly: SnapObjects.Data.dll
Syntax
IDetailTracker<TModel> TrackDetails<TDetailModel>(Expression<Func<TModel, object>> propertyExpr, IEnumerable<IModelEntry<TDetailModel>> modelEntry);
Type Parameters
TDetailModel
The type of the detail 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 a collection of TDetailModel
.
This property is specified by an expression.
modelEntry
System.Collections.Generic.IEnumerable<SnapObjects.Data.IModelEntry<TDetailModel>>
An IEnumerable<IModelEntry<TDetailModel>>
object that contains the data and data state which will be tracked.
Returns
SnapObjects.Data.IDetailTracker<TModel>
Returns the current IDetailTracker<TModel>
object.
Remarks
The ModelEmbedded attribute must be applied in TModel
to create the master-detail relationship between TModel
and TDetailModel
.
The ISqlModelMapper.TrackMaster method can track the master model and return the IDetailTracker<TModel>
object. Then the IDetailTracker.TrackDetails
method can be used to track the detail model object.
One-to-many
The type of the property specified by the propertyExpr
parameter is a collection of TDetailModel
(which represents one or more records in the detail table). The IDetailTracker.TrackDetails
method applies to the one-to-many table relationship (one master table record to many detail table records).
For one-to-one relationship (one master table record to one detail table record), use the IDetailTracker.TrackDetail method instead.
Examples
The following code example demonstrates how to save the master table and the detail table synchronously; the master table has only one affected record and the detail table has two affected records.
using SnapObjects.Data;
using Appeon.ApiDoc.Models.School;
using System;
using System.Collections.Generic;
namespace Appeon.ApiDoc.IDetailTrackerExamples
{
public class TrackDetailsExample
{
private readonly SchoolContext _context;
public TrackDetailsExample(SchoolContext dataContext)
{
// Sets the data context.
_context = dataContext;
}
public void Example(IModelEntry<DepartmentInfo> department,
IEnumerable<IModelEntry<CourseInfo>> coures)
{
// Saves both the master table and the detail table by the data and
// state of the data.
var mapper = _context.SqlModelMapper;
var master = mapper.TrackMaster(department)
.TrackDetails(m => m.Courses, coures)
.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: DepartmentInfo CourseInfo
Applies to
.NET Standard
2.x