diff --git a/src/Persistence/EntityFramework/EntityFrameworkContextBase.cs b/src/Persistence/EntityFramework/EntityFrameworkContextBase.cs
index 50f92c7c4..ae3848467 100644
--- a/src/Persistence/EntityFramework/EntityFrameworkContextBase.cs
+++ b/src/Persistence/EntityFramework/EntityFrameworkContextBase.cs
@@ -2,6 +2,8 @@
// Licensed under the MIT License. See LICENSE file in the project root for full license information.
//
+using System.Threading;
+
namespace MUnique.OpenMU.Persistence.EntityFramework;
using System.Collections;
@@ -61,57 +63,43 @@ protected EntityFrameworkContextBase(DbContext context, IContextAwareRepositoryP
protected IContextAwareRepositoryProvider RepositoryProvider { get; }
///
- public bool SaveChanges()
+ public async ValueTask SaveChangesAsync(CancellationToken cancellationToken = default)
{
- using var l = this._lock.Lock();
+ using var l = await this._lock.LockAsync();
// when we have a change publisher attached, we want to get the changed entries before accepting them.
// Otherwise, we can accept them.
var acceptChanges = true;
+ object? sender = null;
+ SavedChangesEventArgs? args = null;
if (this._changeListener is { })
{
- this.Context.SavedChanges += this.OnSavedChanges;
+ this.Context.SavedChanges += OnSavedChanges;
acceptChanges = false;
}
try
{
- this.Context.SaveChanges(acceptChanges);
+ await this.Context.SaveChangesAsync(acceptChanges, cancellationToken).ConfigureAwait(false);
+
+ if (args is not null)
+ {
+ await this.OnSavedChangesAsync(sender, args).ConfigureAwait(false);
+ }
}
finally
{
- this.Context.SavedChanges -= this.OnSavedChanges;
+ this.Context.SavedChanges -= OnSavedChanges;
}
return true;
- }
-
- ///
- public async ValueTask SaveChangesAsync()
- {
- using var l = await this._lock.LockAsync();
-
- // when we have a change publisher attached, we want to get the changed entries before accepting them.
- // Otherwise, we can accept them.
- var acceptChanges = true;
- if (this._changeListener is { })
+ void OnSavedChanges(object? s, SavedChangesEventArgs e)
{
- this.Context.SavedChanges += this.OnSavedChanges;
- acceptChanges = false;
+ sender = s;
+ args = e;
}
-
- try
- {
- await this.Context.SaveChangesAsync(acceptChanges).ConfigureAwait(false);
- }
- finally
- {
- this.Context.SavedChanges -= this.OnSavedChanges;
- }
-
- return true;
}
///
@@ -270,7 +258,6 @@ protected virtual void Dispose(bool dispose)
return;
}
- this.Context.SavedChanges -= this.OnSavedChanges;
this.Context.Dispose();
}
@@ -319,7 +306,7 @@ private void ForEachAggregate(object obj, Action