Show / Hide Table of Contents

    ISqlModelMapper.LoadByKey<TModel>(params object[] parameters) Method

    .NET Standard 2.x

    Retrieves data according to the primary key defined in a TModel class.

    Namespace: SnapObjects.Data

    Assembly: SnapObjects.Data.dll

    Syntax

      public ILoadable<TModel> LoadByKey<TModel>(params object[] parameters);
    

    Type Parameters

    TModel

    The type of a model class.

    Parameters

    parameters System.Object[]

    (Optional) One or more values that you want to use as retrieval arguments in the SQL SELECT statement defined by the primary table and its primary key(s) in TModel.

    Returns

    SnapObjects.Data.ILoadable<TModel>

    Returns the ILoadable<TModel> interface whose methods can be used to further obtain the result set, or perform data operations such as embedded queries.

    Remarks

    If the SQL query defined in TModel makes query from multiple tables, then this method only retrieves and loads data from the primary table.

    If TModel has properties that apply attributes such as SqlCompute, NotMapped etc., LoadAll and LoadByKey cannot load data of such properties. In such case, use Load instead of LoadAll and LoadByKey.

    Examples

    The following code example demonstrates how to load the department according to the key.

    using Appeon.ApiDoc.Models.School;
    using System;
    
    namespace Appeon.ApiDoc.ISqlModelMapperExamples
    {
        public class LoadByKeyExample
        {
            private SchoolContext _context;
    
            public LoadByKeyExample(SchoolContext dataContext)
            {
                // Sets the data context.
                _context = dataContext;
            }
    
            public void Example()
            {
                var mapper = _context.SqlModelMapper;
    
                // Loads the department of Engineering according to the key (Department ID = 1).
                var engineering = mapper.LoadByKey<Department>(1)
                                        .FirstOrDefault();
    
                Console.WriteLine("The department of Engineering:");
                Console.WriteLine("Department ID is {0}",
                    engineering.DepartmentID);
                Console.WriteLine("Name is {0}", 
                    engineering.Name);
                Console.WriteLine("Budget is {0}", 
                    engineering.Budget.ToString("N0"));
    
                /*The code produces the following output:
                 
                The department of Engineering:
                Department ID is 1
                Name is Engineering
                Budget is 350,000
                */
            }
        }
    }
    

    Example Refer To

    Model Class: Department

    Applies to

    .NET Standard

    2.x

    Back to top Generated by Appeon