Show / Hide Table of Contents

    ISqlModelMapper.LoadAsync<TModel>(object[] parameters, CancellationToken cancellationToken) Method

    .NET Standard 2.x

    Asynchronously retrieves data according to the SQL query defined in a TModel class.

    Namespace: SnapObjects.Data

    Assembly: SnapObjects.Data.dll

    Syntax

    public Task<ILoadable<TModel>> LoadAsync<TModel>(object[] parameters, CancellationToken cancellationToken)
    

    Type Parameters

    TModel

    The type of a model class.

    Parameters

    parameters System.Object[]

    One or more values that you want to use as retrieval arguments in the SQL SELECT statement defined in TModel.

    cancellationToken CancellationToken

    A cancellation token that can be used by other objects or threads to receive notice of cancellation.

    Returns

    Task<ILoadable<TModel>>

    Returns a task that represents the asynchronous operation.

    Examples

    The following code example demonstrates how to load departments asynchronously according to a model class. It uses a CancellationTokenSource type parameter to make this operation cancelable.

    using System;
    using System.Collections.Generic;
    using System.Threading;
    using System.Threading.Tasks;
    using Appeon.ApiDoc.Models.School;
    
    namespace Appeon.ApiDoc.ISqlModelMapperExamples
    {
        public class LoadAsyncExample
        {
            private readonly SchoolContext _context;
            
            public LoadAsyncExample(SchoolContext dataContext)
            {
                // Sets the data context.
                _context = dataContext;
            }
    
            public async Task<DepartmentByName> Example2(CancellationTokenSource cts)
            {
                var mapper = _context.SqlModelMapper;
                
                DepartmentByName depts = null;
                
                try
                {
                    // Asynchronously loads the department of Engineering.
                    // If a task has been cancelled, the call to the ISqlModelMapper.LoadAsync 
                    // method throws an OperationCanceledException.
                    depts = (await mapper.LoadAsync<DepartmentByName>(
                        new object[] { "Engineering" } , cts.Token)).FirstOrDefault();
                        
                    Console.WriteLine();
                    Console.WriteLine("The department of Engineering:");
                    Console.WriteLine("Department ID is {0}", depts.DepartmentID);
                    Console.WriteLine("Name is {0}", depts.Name);
                    Console.WriteLine("Budget is {0}", depts.Budget.ToString("N0"));
                }
                catch (OperationCanceledException e)
                {
                    // The operation was cancelled before completion.
                    Console.WriteLine(e.Message);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
                finally
                {
                    cts.Dispose();
                }
                
                return depts;
                
                /* This code produces the following output:
                
                    A task was canceled.
                */
            }
        }
    }
    

    Example Refer To

    Model Classes: Department DepartmentByName

    Applies to

    .NET Standard

    2.x

    Back to top Generated by Appeon