Esempio n. 1
0
        public static async Task SaveChangesWithConcurrencyConflictsResolvingAsync(this ISqlServerDbContext dbContext,
                                                                                   CancellationToken cancellationToken = default)
        {
            if (dbContext is null)
            {
                throw new ArgumentNullException(nameof(dbContext));
            }

            while (true)
            {
                try
                {
                    await dbContext.SaveChangesAsync(cancellationToken).ConfigureAwait(false);

                    break;
                }
                catch (DbUpdateConcurrencyException ex)
                {
                    if (ex.Entries.Count == 0)
                    {
                        throw;
                    }

                    foreach (var entry in ex.Entries)
                    {
                        entry.State = EntityState.Detached;
                    }
                }
            }
        }