Show / Hide Table of Contents

    ISqlModelMapper.LoadAllAsync<TModel>() Method

    .NET Standard 2.x

    Asynchronously retrieves data from the database according to the SQL query (without the Where clause) defined by the primary table in a TModel class.

    Namespace: SnapObjects.Data

    Assembly: SnapObjects.Data.dll

    Syntax

    public Task<ILoadable<TModel>> LoadAllAsync<TModel>()
    

    Type Parameters

    TModel

    The type of a model class.

    Returns

    Task<ILoadable<TModel>>

    Returns a task that represents the asynchronous operation.

    Remarks

    If the SQL query defined in TModel makes query from multiple tables, 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 departments asynchronously without the Where clause according to a model class.

    using System;
    using System.Threading;
    using System.Threading.Tasks;
    using Appeon.ApiDoc.Models.School;
    
    namespace Appeon.ApiDoc.ISqlModelMapperExamples
    {
        public class LoadAllAsyncExample
        {
            private readonly SchoolContext _context;
            
            public LoadAllAsyncExample(SchoolContext dataContext)
            {
                // Sets the data context.
                _context = dataContext;
            }
    
            public async Task<int> Example1()
            {
                var mapper = _context.SqlModelMapper;
                
                // Asynchronously loads the department of Engineering.
                // Passes in the argument by a value
                int count = (await mapper.LoadAsync<DepartmentByName>("Engineering")).ToList().Count;
                
                Console.WriteLine("Count (LoadAsync): {0}", count);
                
                // Asynchronously lLoads the department of Engineering using LoadAll
                // It retrieves all records from Department table. 
                count = (await mapper.LoadAllAsync<DepartmentByName>()).ToList().Count;
                
                Console.WriteLine("Count (LoadAllAsync): {0}", count);
                
                return count;
                
                /* The code produces the following output:
                
                Count (LoadAsync): 1
                Count (LoadAllAsync): 4
                */
            }
        }
    }
    

    Example Refer To

    Model Class: DepartmentByName

    Applies to

    .NET Standard

    2.x

    Back to top Generated by Appeon