Show / Hide Table of Contents

    JoinTableAttribute Class

    .NET Standard 2.x | Current Version (1.0.1)

    0.5.0-alpha

    1.0.1 (current)

    Sets the joined table to be used in a model class. Multiple JoinTable attributes can be applied to one model class.

    Namespace: SnapObjects.Data

    Assembly: SnapObjects.Data.dll

    Inherited Constructor

    System.ComponentModel.DataAnnotations.Schema.TableAttribute

    Syntax

       [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct, AllowMultiple = true)]
       public class JoinTableAttribute : TableAttribute, IJoinTable
    

    Constructors

    Name Description
    JoinTableAttribute(string name) Initializes a new instance of the JoinTableAttribute class.
    JoinTableAttribute(string alias, string name) Initializes a new instance of the JoinTableAttribute class.

    Properties

    Name Return Type Description
    Alias string Gets the alias of the joined table.
    AliasDefined bool Gets whether the joined table has an alias or not.
    JoinType SqlJoinType Gets or sets the type of join operation to be used in the FROM clause.
    Name String Gets the name of the table to join.
    OnRaw string Gets or sets the raw SQL condition from which the join is based.
    Schema String Gets or sets the schema of the table to join.

    Remarks

    The JoinTable attribute is equivalent to applying the join type (INNER JOIN, FULL OUTER JOIN, LEFT OUTER JOIN, RIGHT OUTER JOIN, etc.) of the SQL Select statement to the model so as to define the joining relationship between tables.

    Examples

    Example 1

    The following code example demonstrates how to use the constructor: JoinTable (name, Propreties: Schema, JoinType, OnRaw) to apply the JoinTable attribute to CourseJoinTable class. It uses LEFT JOIN to join Course table with StudentGrade table, and specifies the raw SQL search condition "Course.CourseID = StudentGrade.CourseID" on which the join is based.

    Model: CourseJoinTable
    using SnapObjects.Data;
    using System;
    
    namespace Appeon.ApiDoc.Models.School
    {
        /// <summary>
        /// This model class maps to the dbo.Course and dbo.StudentGrade tables.
        /// </summary>
        [FromTable("Course", Schema = "dbo")]
        [JoinTable("StudentGrade", Schema = "dbo", JoinType =SqlJoinType.Left,
            OnRaw = "Course.CourseID = StudentGrade.CourseID")]
        public class CourseJoinTable
        {
            public Int32 CourseID { get; set; }
    
            public String Title { get; set; }
    
            [SqlColumn("StudentGrade", column: "Grade")]
            public Decimal? Grade { get; set; }
    
        }
    }
    
    Example Method: Example1
    using SnapObjects.Data;
    using Appeon.ApiDoc.Models.School;
    using System;
    
    namespace Appeon.ApiDoc.JoinTableAttributeExamples
    {
        public class JoinTableAttributeExample
        {
            private SchoolContext _context;
    
            public JoinTableAttributeExample(SchoolContext dataContext)
            {
                // Sets the data context.
                _context = dataContext;
            }
    
            public void Example1()
            {
                // Gets the SQL statement.
                string sql = ModelSqlBuilder.
                    GetBuilder<CourseJoinTable>(_context).QuerySql;
    
                Console.WriteLine("SQL statement:");
                Console.WriteLine(sql);
    
                // Executes the SQL by a SqlExecutor object. 
                var resultSet = _context.SqlExecutor.Select<CourseJoinTable>(sql);
    
                /* This code example produces the following output:
                
                SQL statement:
                SELECT
                 [dbo].[Course].[CourseID],
                [dbo].[Course].[Title],
                [StudentGrade].[Grade]
                FROM [dbo].[Course]
                LEFT JOIN [dbo].[StudentGrade]
                ON [Course].[CourseID] = [StudentGrade].[CourseID]
                */
            }
        }
    }
    

    Example 2

    The following code example demonstrates how to use the constructor: JoinTable (alias, name, Propreties: Schema, JoinType, OnRaw) to apply the JoinTable attribute to CourseJoinTable2 class. It specifies an alias for the StudentGrade table.

    Model: CourseJoinTable2
    using SnapObjects.Data;
    using System;
    
    namespace Appeon.ApiDoc.Models.School
    {
        /// <summary>
        /// This model class maps to the dbo.Course and dbo.StudentGrade tables.
        /// </summary>
        [FromTable("Course", Schema = "dbo")]
        [JoinTable("s", "StudentGrade", Schema = "dbo", JoinType = SqlJoinType.Left,
            OnRaw = "Course.CourseID = s.CourseID")]
        public class CourseJoinTable2
        {
            public Int32 CourseID { get; set; }
    
            public String Title { get; set; }
    
            [SqlColumn("s", column: "Grade")]
            public Decimal? Grade { get; set; }
    
        }
    }
    
    Example Method: Example2
            public void Example2()
            {
                // Gets the SQL statement.
                string sql = ModelSqlBuilder.GetBuilder<CourseJoinTable2>(_context).QuerySql;
    
                Console.WriteLine("SQL statement:");
                Console.WriteLine(sql);
    
                // Executes the SQL by a SqlExecutor object. 
                var resultSet = _context.SqlExecutor.Select<CourseJoinTable2>(sql);
    
                /* This code example produces the following output:
                
                SQL statement:
                SELECT
                 [dbo].[Course].[CourseID],
                [dbo].[Course].[Title],
                [s].[Grade]
                FROM [dbo].[Course]
                LEFT JOIN [dbo].[StudentGrade] AS [s]
                ON [Course].[CourseID] = [s].[CourseID]
                */
            }
    

    Applies to

    .NET Standard

    2.x

    Back to top Generated by Appeon