diff --git a/README.md b/README.md index 9df0686..d28fd82 100644 --- a/README.md +++ b/README.md @@ -61,6 +61,7 @@ To use Raiqub.Expressions in your project, follow these steps: For Entity Framework Core: ```csharp + // The DbContext must be registered calling AddDbContextFactory() services.AddEntityFrameworkExpressions() .AddSingleContext(); ``` diff --git a/src/Expressions.EntityFrameworkCore/ExpressionsSessionBuilder.cs b/src/Expressions.EntityFrameworkCore/ExpressionsSessionBuilder.cs index d9bd773..2321583 100644 --- a/src/Expressions.EntityFrameworkCore/ExpressionsSessionBuilder.cs +++ b/src/Expressions.EntityFrameworkCore/ExpressionsSessionBuilder.cs @@ -32,9 +32,9 @@ public void AddSingleContext(ChangeTracking? tracking = null) { var combinedTracking = tracking ?? _tracking; - _services.AddScoped>(); - _services.AddTransient(sp => sp.GetRequiredService>()); - _services.AddTransient(sp => sp.GetRequiredService>()); + _services.AddSingleton>(); + _services.AddSingleton(sp => sp.GetRequiredService>()); + _services.AddSingleton(sp => sp.GetRequiredService>()); _services.AddScoped(sp => sp.GetRequiredService().Create(combinedTracking)); _services.AddScoped(sp => sp.GetRequiredService().Create()); } @@ -52,9 +52,9 @@ public ExpressionsSessionBuilder AddContext(ChangeTracking { var combinedTracking = tracking ?? _tracking; - _services.AddScoped>(); - _services.AddTransient>(sp => sp.GetRequiredService>()); - _services.AddTransient>( + _services.AddSingleton>(); + _services.AddSingleton>(sp => sp.GetRequiredService>()); + _services.AddSingleton>( sp => sp.GetRequiredService>()); _services.AddScoped>( sp => sp.GetRequiredService>().Create(combinedTracking)); diff --git a/src/Expressions.EntityFrameworkCore/Sessions/EFSession.cs b/src/Expressions.EntityFrameworkCore/Sessions/EFSession.cs index 8060454..f950bd1 100644 --- a/src/Expressions.EntityFrameworkCore/Sessions/EFSession.cs +++ b/src/Expressions.EntityFrameworkCore/Sessions/EFSession.cs @@ -88,17 +88,16 @@ public void Dispose() GC.SuppressFinalize(this); } - protected virtual ValueTask DisposeAsyncCore() + protected virtual async ValueTask DisposeAsyncCore() { - Context.ChangeTracker.Clear(); - return new ValueTask(); + await Context.DisposeAsync(); } protected virtual void Dispose(bool disposing) { if (disposing) { - Context.ChangeTracker.Clear(); + Context.Dispose(); } } } diff --git a/src/Expressions.EntityFrameworkCore/Sessions/EFSessionFactory.cs b/src/Expressions.EntityFrameworkCore/Sessions/EFSessionFactory.cs index 44d6364..05cbc64 100644 --- a/src/Expressions.EntityFrameworkCore/Sessions/EFSessionFactory.cs +++ b/src/Expressions.EntityFrameworkCore/Sessions/EFSessionFactory.cs @@ -10,17 +10,17 @@ public class EFSessionFactory where TContext : DbContext { private readonly ILoggerFactory _loggerFactory; - private readonly TContext _context; + private readonly IDbContextFactory _contextFactory; - public EFSessionFactory(ILoggerFactory loggerFactory, TContext context) + public EFSessionFactory(ILoggerFactory loggerFactory, IDbContextFactory contextFactory) { _loggerFactory = loggerFactory; - _context = context; + _contextFactory = contextFactory; } public EFSession Create(ChangeTracking? tracking = null) => new( _loggerFactory.CreateLogger>(), - _context, + _contextFactory.CreateDbContext(), tracking ?? ChangeTracking.Default); public EFSession CreateForQuery() => Create(ChangeTracking.Disable); diff --git a/src/Expressions.Marten/ExpressionsSessionBuilder.cs b/src/Expressions.Marten/ExpressionsSessionBuilder.cs index 10a7c1b..d3f0c4c 100644 --- a/src/Expressions.Marten/ExpressionsSessionBuilder.cs +++ b/src/Expressions.Marten/ExpressionsSessionBuilder.cs @@ -29,9 +29,9 @@ public void AddSingleContext(ChangeTracking? tracking = null) { var combinedTracking = tracking ?? _tracking; - _services.AddScoped(); - _services.AddTransient(sp => sp.GetRequiredService()); - _services.AddTransient(sp => sp.GetRequiredService()); + _services.AddSingleton(); + _services.AddSingleton(sp => sp.GetRequiredService()); + _services.AddSingleton(sp => sp.GetRequiredService()); _services.AddScoped(sp => sp.GetRequiredService().Create(combinedTracking)); _services.AddScoped(sp => sp.GetRequiredService().Create()); } @@ -49,9 +49,9 @@ public ExpressionsSessionBuilder AddContext(ChangeTracking { var combinedTracking = tracking ?? _tracking; - _services.AddScoped>(); - _services.AddTransient>(sp => sp.GetRequiredService>()); - _services.AddTransient>( + _services.AddSingleton>(); + _services.AddSingleton>(sp => sp.GetRequiredService>()); + _services.AddSingleton>( sp => sp.GetRequiredService>()); _services.AddScoped>( sp => sp.GetRequiredService>().Create(combinedTracking)); diff --git a/src/Expressions.Marten/Sessions/MartenQuerySession.cs b/src/Expressions.Marten/Sessions/MartenQuerySession.cs index f487022..8003608 100644 --- a/src/Expressions.Marten/Sessions/MartenQuerySession.cs +++ b/src/Expressions.Marten/Sessions/MartenQuerySession.cs @@ -42,9 +42,9 @@ public void Dispose() GC.SuppressFinalize(this); } - protected virtual ValueTask DisposeAsyncCore() + protected virtual async ValueTask DisposeAsyncCore() { - return _session.DisposeAsync(); + await _session.DisposeAsync(); } protected virtual void Dispose(bool disposing) diff --git a/src/Expressions.Marten/Sessions/MartenSessionFactory.cs b/src/Expressions.Marten/Sessions/MartenSessionFactory.cs index 01390ae..3b82513 100644 --- a/src/Expressions.Marten/Sessions/MartenSessionFactory.cs +++ b/src/Expressions.Marten/Sessions/MartenSessionFactory.cs @@ -32,7 +32,7 @@ internal static IDocumentSession CreateSession(IDocumentStore documentStore, Cha { return tracking switch { - ChangeTracking.Default => documentStore.IdentitySession(), + ChangeTracking.Default => documentStore.LightweightSession(), ChangeTracking.Enable => documentStore.DirtyTrackedSession(), ChangeTracking.IdentityResolution => documentStore.IdentitySession(), ChangeTracking.Disable => documentStore.LightweightSession(), diff --git a/tests/Expressions.EntityFrameworkCore.Tests/DatabaseServiceCollectionExtensions.cs b/tests/Expressions.EntityFrameworkCore.Tests/DatabaseServiceCollectionExtensions.cs index bd361e8..5dbdae1 100644 --- a/tests/Expressions.EntityFrameworkCore.Tests/DatabaseServiceCollectionExtensions.cs +++ b/tests/Expressions.EntityFrameworkCore.Tests/DatabaseServiceCollectionExtensions.cs @@ -18,7 +18,7 @@ public static IServiceCollection AddSqliteDbContext(this IServiceColle .UseSqlite(sp.GetRequiredService()) .Options); - services.AddDbContext(); + services.AddDbContextFactory(); return services; }