ISqlModelMapper.LoadAllAsync<TModel>(CancellationToken cancellationToken) 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>(CancellationToken cancellationToken)
Type Parameters
TModel
The type of a model class.
Parameters
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.
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. It uses a CancellationTokenSource type parameter to make this operation cancelable.
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> Example2(CancellationTokenSource cts)
{
var mapper = _context.SqlModelMapper;
int count = 0;
try
{
// Asynchronously lLoads the department of Engineering using LoadAll
// It retrieves all records from Department table.
// If a task has been cancelled, the call to the ISqlModelMapper.LoadAsync
// method throws an OperationCanceledException.
count = (await mapper.LoadAllAsync<DepartmentByName>(cts.Token)).ToList().Count;
Console.WriteLine("Count (LoadAllAsync): {0}", count);
}
catch (OperationCanceledException e)
{
// The operation was cancelled before completion.
Console.WriteLine(e.Message);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
finally
{
cts.Dispose();
}
return count;
/* The code produces the following output:
A task was canceled.
*/
}
}
}
Example Refer To
Model Class: DepartmentByName
Applies to
.NET Standard
2.x