Show / Hide Table of Contents

    IDetailTracker.TrackDetail<TDetailModel>(Expression<Func<TModel, object>> propertyExpr, IModelEntry<TDetailModel> modelEntry) Method

    .NET Standard 2.x

    Tracks an insert, update or delete operation 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 TDetailModel object.

    When ISqlModelMapper.SaveChanges method is called, a SQL statement (INSERT, UPDATE, or DELETE) will be first generated using the data cached in the TDetailModel object and the mapping information defined in TDetailModel, and then executed.

    Namespace: SnapObjects.Data

    Assembly: SnapObjects.Data.dll

    Syntax

      IDetailTracker<TModel> TrackDetail<TDetailModel>(Expression<Func<TModel, object>> propertyExpr, 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 TDetailModel.

    This property is specified by an expression.

    modelEntry SnapObjects.Data.IModelEntry<TDetailModel>

    An IModelEntry<TDetailModel> object which contains the data and data state and which will be tracked.

    Returns

    SnapObjects.Data.IDetailTracker<TModel>

    Returns the current IDetailTracker<TModel> object.

    Remarks

    The ModelEmbedded attribute must be applied to a property 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.TrackDetail method can be used to track the detail model.

    One-to-one

    The type of the property specified by the propertyExpr parameter is TDetailModel (this property represents one row in the detail table). The IDetailTracker.TrackDetail method only applies to the one-to-one table relationship (one master table record to one detail table record).

    For one-to-many relationship (one master table record to many detail table records), use the IDetailTracker.TrackDetails method instead.

    Examples

    The following code example demonstrates how to save the master table and the detail table synchronously and both the master table and the detail table have only one affected record.

    using SnapObjects.Data;
    using Appeon.ApiDoc.Models.School;
    using System;
    
    namespace Appeon.ApiDoc.IDetailTrackerExamples
    {
        public class TrackDetailExample
        {
            private readonly SchoolContext _context;
    
            public TrackDetailExample(SchoolContext dataContext)
            {
                // Sets the data context.
                _context = dataContext;
            }
    
            public void Example(IModelEntry<CourseAndOnlineInfo> course,
                IModelEntry<OnlineCourse> onlineCourse)
            {
                // Saves both the master table and the detail table and both have only one affected record.
                var mapper = _context.SqlModelMapper;
    
                var master = mapper.TrackMaster(course)
                                   .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);
    
                /* This 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

    See Also

    ISqlModelMapper.TrackMaster

    Back to top Generated by Appeon