-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
74f8bf5
commit e9a1508
Showing
7 changed files
with
68 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Reflection; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.EntityFrameworkCore.Infrastructure; | ||
|
||
namespace Stl.Fusion.EntityFramework | ||
{ | ||
/// <summary> | ||
/// This type solves a single problem: currently EF Core 6.0 | ||
/// doesn't properly dispose pooled DbContexts rendering | ||
/// them unusable after disposal. | ||
/// Details: https://github.com/dotnet/efcore/issues/26202 | ||
/// </summary> | ||
public abstract class DbContextBase : | ||
#if NET6_0 | ||
DbContext, IResettableService | ||
#else | ||
DbContext | ||
#endif | ||
{ | ||
#if NET6_0 | ||
private static readonly FieldInfo DisposedField = | ||
typeof(DbContext).GetField("_disposed", BindingFlags.Instance | BindingFlags.NonPublic)!; | ||
private static readonly MethodInfo GetResettableServicesMethod = | ||
typeof(DbContext).GetMethod("GetResettableServices", BindingFlags.Instance | BindingFlags.NonPublic)!; | ||
#endif | ||
|
||
protected DbContextBase() { } | ||
protected DbContextBase(DbContextOptions options) : base(options) { } | ||
|
||
#if NET6_0 | ||
void IResettableService.ResetState() | ||
{ | ||
var services = GetResettableServices2(); | ||
foreach (var service in services) | ||
service.ResetState(); | ||
DisposedField.SetValue(this, false); | ||
} | ||
|
||
async Task IResettableService.ResetStateAsync(CancellationToken cancellationToken = default) | ||
{ | ||
var services = GetResettableServices2(); | ||
foreach (var service in services) | ||
await service.ResetStateAsync(cancellationToken).ConfigureAwait(false); | ||
DisposedField.SetValue(this, false); | ||
} | ||
|
||
private IEnumerable<IResettableService> GetResettableServices2() | ||
=> (IEnumerable<IResettableService>) GetResettableServicesMethod.Invoke( | ||
this, | ||
Array.Empty<object>())!; | ||
#endif | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters