Show / Hide Table of Contents

    ModelEmbeddedAttribute Class

    .NET Standard 2.x

    Specifies that a property in the model class will use the specified model class to load and update data independently.

    Namespace: SnapObjects.Data

    Assembly: SnapObjects.Data.dll

    Inheritance Constructor

    System.Attribute

    Syntax

       [AttributeUsage(AttributeTargets.Property)]
       public class ModelEmbeddedAttribute : Attribute, IModelEmbedded
    

    Constructors

    Name Description
    ModelEmbeddedAttribute(Type modelType) Initializes a new instance of the ModelEmbeddedAttribute class.

    Properties

    Name Return Type Description
    CascadeCreate bool Gets or sets whether to perform the insert operation for the current property (using definitions of the detail model) when the current model is inserting data.
    CascadeDelete bool Gets or sets whether to perform the delete operation for the current property (using definitions of the detail model) when the current model is deleting data.
    ModelType Type Gets the type of a model class which the property relies on to load and update data.
    ParamValue string Gets or sets the value of the parameter that is required when loading the data for the current property.
    QueryExpression string Sets the SQL expression parameter that corresponds to the query method of the QuerySelector property.
    QuerySelector QuerySelector Gets or sets the query method to be used when loading the data for the current property.

    Examples

    The following code example demonstrates how to use the ModelEmbedded attribute. It applies the ModelEmbedded attribute to the OnlineCourse property in the CourseAndOnlineInfo class, to make the OnlineCourse property as a reference to the OnlineCourse object.

    It defines the master-detail relationship between Course and OnlineCourse, Course (represented by CourseAndOnlineInfo class) is the master model, OnlineCourse (represented by OnlineCourse class) is the detail model.

    Model: CourseAndOnlineInfo
    using System;
    using System.ComponentModel.DataAnnotations;
    using System.ComponentModel.DataAnnotations.Schema;
    using SnapObjects.Data;
    using Newtonsoft.Json;
    using System.Collections.Generic;
    
    namespace Appeon.ApiDoc.Models.School
    {
        /// <summary>
        /// This model class maps to the dbo.Course table. 
        /// It uses the primary key to generate the Where clause for the Update operation.
        /// 
        /// CourseID is the primary key.
        /// 
        /// The OnlineCourses property is applied with the ModelEmbedded attribute.
        /// The ModelEmbedded attribute defines the one-to-one relationship between the Course and 
        /// the OnlineCourse record.
        /// </summary>
        [Table("Course", Schema = "dbo")]
        [UpdateWhereStrategy(UpdateWhereStrategy.KeyColumns)]
        public class CourseAndOnlineInfo
        {
            [Key]
            public int CourseID { get; set; }
    
            public string Title { get; set; }
    
            public int Credits { get; set; }
    
            public int DepartmentID { get; set; }
    
            [JsonIgnore]
            [SetValue("$CourseID", "$CourseID", SetValueStrategy.Always)]
            [ModelEmbedded(typeof(OnlineCourse), ParamValue = "$CourseID", 
                CascadeCreate = true, CascadeDelete = true)]
            public OnlineCourse OnlineCourse { get; set; }
        }
    }
    
    Model: OnlineCourse
    using System;
    using System.ComponentModel.DataAnnotations;
    using System.ComponentModel.DataAnnotations.Schema;
    using SnapObjects.Data;
    
    namespace Appeon.ApiDoc.Models.School
    {
        /// <summary>
        /// This model class maps to the dbo.OnlineCourse table. 
        /// It uses the primary key to generate the Where clause for the Update operation.
        /// 
        /// CourseID is the primary key.
        /// </summary>
        [Table("OnlineCourse", Schema = "dbo")]
        [UpdateWhereStrategy(UpdateWhereStrategy.KeyColumns)]
        public class OnlineCourse
        {
            [Key]
            public int CourseID { get; set; }
    
            public string URL { get; set; }
    
        }
    }
    
    Example Method:
    using SnapObjects.Data;
    using Appeon.ApiDoc.Models.School;
    using System;
    
    namespace Appeon.ApiDoc.ModelEmbeddedAttributeExamples
    {
        public class ModelEmbeddedAttributeExample
        {
            private SchoolContext _context;
    
            public ModelEmbeddedAttributeExample(SchoolContext dataContext)
            {
                // Sets the data context.
                _context = dataContext;
            }
    
            public void Example(IModelEntry<CourseAndOnlineInfo> course,
                IModelEntry<OnlineCourse> onlineCourse)
            {
    
                var mapper = _context.SqlModelMapper;
    
                /// It tracks the data and state for Course (CourseAndOnlineInfo class),
                /// then tracks the data and state for OnlineCourse (OnlineCourse class)
                /// by the OnlineCourse property of the CourseAndOnlineInfo class.
                var master =
                    mapper.TrackMaster(course)
                          .TrackDetail(m => m.OnlineCourse, onlineCourse)
                          .MasterModel;
    
                // Saves changes to database.
                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.
                */
            }
        }
    }
    

    Applies to

    .NET Standard

    2.x

    Back to top Generated by Appeon