Show / Hide Table of Contents

    ISqlModelMapper.SaveChangesAsync(CancellationToken cancellationToken = default) Method

    .NET Standard 2.x

    Asynchronously saves all of the data changes to the database, by executing the database table operations tracked by the ISqlModelMapper object, and calls the tracked Action delegates during operations.

    Namespace: SnapObjects.Data

    Assembly: SnapObjects.Data.dll

    Syntax

    public Task<IDbResult> SaveChangesAsync(CancellationToken cancellationToken = default)
    

    Parameters

    cancellationToken CancellationToken

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

    Returns

    Task<IDbResult>

    Returns a task that represents the asynchronous operation.

    Remarks

    When the database is updated successfully, SqlModelMapper is restored to its initial state which will not track data changes.

    To continue using SqlModelMapper for other data operations, use ISqlModelMapper.TrackCreate method etc. to track the changes again.

    Database Transaction

    This method will create by default a database transaction where all of the SQL DML will be executed.

    When any exceptions occur, the database transaction will be automatically rolled back.

    Validation

    Before updating the database, the system will validate the data in the model. If detects any error, exceptions will be thrown.

    Examples

    The following code example demonstrates how to register two new students. 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;
    using SnapObjects.Data;
    
    namespace Appeon.ApiDoc.ISqlModelMapperExamples
    {
        public class SaveChangesAsyncExample
        {
            private readonly SchoolContext _context;
            
            public SaveChangesAsyncExample(SchoolContext dataContext)
            {
                // Sets the data context.
                _context = dataContext;
            }
    
            public async Task<int> Example2(CancellationTokenSource cts)
            {
                var mapper = _context.SqlModelMapper;
                
                // Tracks the two new students and saves changes to the database asynchronously.
                var personA = new Person()
                {
                    LastName = "Senior",
                    FirstName = "Letitia",
                    EnrollmentDate = DateTime.Now,
                    Discriminator = "Student"
                };
                
                var personB = new Person()
                {
                    LastName = "Ward",
                    FirstName = "Lilith",
                    EnrollmentDate = DateTime.Now,
                    Discriminator = "Student"
                };
                
                IDbResult dbResult = null;
                
                try
                {
                    // If a task has been cancelled, the call to the ISqlModelMapper.LoadAsync 
                    // method throws an OperationCanceledException.
                    dbResult = await mapper.TrackCreate(personA)
                                            .TrackCreate(personB)
                                            .SaveChangesAsync(cts.Token);
                                            
                    Console.WriteLine("{0} records have been inserted into the database.",
                        dbResult.InsertedCount);
                }
                catch (OperationCanceledException e)
                {
                    // The operation was cancelled before completion.
                    Console.WriteLine(e.Message);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
                finally
                {
                    cts.Dispose();
                }
                
                return dbResult.AffectedCount;
                
                /* The code produces the following output:
                
                    A task was canceled.
                */
            }
        }
    }
    

    Applies to

    .NET Standard

    2.x

    Back to top Generated by Appeon