|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using Microsoft.EntityFrameworkCore.Internal; |
| 5 | + |
| 6 | +namespace Microsoft.EntityFrameworkCore.Infrastructure; |
| 7 | + |
| 8 | +public class AddPooledDbContextFactoryParameterlessTest |
| 9 | +{ |
| 10 | + private sealed class TestDbContext : DbContext |
| 11 | + { |
| 12 | + public TestDbContext(DbContextOptions<TestDbContext> options) : base(options) { } |
| 13 | + } |
| 14 | + |
| 15 | + [Fact] |
| 16 | + public async Task Parameterless_factory_should_use_ConfigureDbContext_options() |
| 17 | + { |
| 18 | + var services = new ServiceCollection(); |
| 19 | + |
| 20 | + services.ConfigureDbContext<TestDbContext>((sp, opts) => |
| 21 | + opts.UseInMemoryDatabase("test_db")); |
| 22 | + |
| 23 | + services.AddPooledDbContextFactory<TestDbContext>(); |
| 24 | + |
| 25 | + using var provider = services.BuildServiceProvider(); |
| 26 | + var factory = provider.GetRequiredService<IDbContextFactory<TestDbContext>>(); |
| 27 | + await using var db = await factory.CreateDbContextAsync(); |
| 28 | + |
| 29 | + Assert.Equal("Microsoft.EntityFrameworkCore.InMemory", db.Database.ProviderName); |
| 30 | + } |
| 31 | + |
| 32 | + [Fact] |
| 33 | + public async Task Parameterless_factory_with_custom_pool_size_should_still_resolve() |
| 34 | + { |
| 35 | + var services = new ServiceCollection(); |
| 36 | + |
| 37 | + services.ConfigureDbContext<TestDbContext>((sp, opts) => |
| 38 | + opts.UseInMemoryDatabase("test_db_custom_pool")); |
| 39 | + |
| 40 | + services.AddPooledDbContextFactory<TestDbContext>(poolSize: 256); |
| 41 | + |
| 42 | + using var provider = services.BuildServiceProvider(); |
| 43 | + var factory = provider.GetRequiredService<IDbContextFactory<TestDbContext>>(); |
| 44 | + await using var db = await factory.CreateDbContextAsync(); |
| 45 | + |
| 46 | + Assert.Equal("Microsoft.EntityFrameworkCore.InMemory", db.Database.ProviderName); |
| 47 | + } |
| 48 | + |
| 49 | + [Fact] |
| 50 | + public void Scoped_resolution_of_TContext_uses_pooled_factory() |
| 51 | + { |
| 52 | + var services = new ServiceCollection(); |
| 53 | + |
| 54 | + services.ConfigureDbContext<TestDbContext>((sp, opts) => |
| 55 | + opts.UseInMemoryDatabase("scoped_db")); |
| 56 | + |
| 57 | + services.AddPooledDbContextFactory<TestDbContext>(); |
| 58 | + |
| 59 | + using var provider = services.BuildServiceProvider(); |
| 60 | + |
| 61 | + using var scope = provider.CreateScope(); |
| 62 | + var ctx = scope.ServiceProvider.GetRequiredService<TestDbContext>(); |
| 63 | + Assert.Equal("Microsoft.EntityFrameworkCore.InMemory", ctx.Database.ProviderName); |
| 64 | + } |
| 65 | + |
| 66 | + [Fact] |
| 67 | + public void Pooled_services_are_registered_and_singleton() |
| 68 | + { |
| 69 | + var services = new ServiceCollection(); |
| 70 | + |
| 71 | + services.ConfigureDbContext<TestDbContext>((sp, opts) => |
| 72 | + opts.UseInMemoryDatabase("pool_reg_db")); |
| 73 | + |
| 74 | + services.AddPooledDbContextFactory<TestDbContext>(); |
| 75 | + |
| 76 | + using var provider = services.BuildServiceProvider(); |
| 77 | + |
| 78 | + var pool1 = provider.GetRequiredService<IDbContextPool<TestDbContext>>(); |
| 79 | + var pool2 = provider.GetRequiredService<IDbContextPool<TestDbContext>>(); |
| 80 | + |
| 81 | + // Should be the same singleton instance |
| 82 | + Assert.Same(pool1, pool2); |
| 83 | + |
| 84 | + // And the factory should resolve |
| 85 | + var factory = provider.GetRequiredService<IDbContextFactory<TestDbContext>>(); |
| 86 | + Assert.NotNull(factory); |
| 87 | + } |
| 88 | + |
| 89 | + [Fact] |
| 90 | + public async Task Parameterless_factory_without_configuration_throws_meaningful_error() |
| 91 | + { |
| 92 | + var services = new ServiceCollection(); |
| 93 | + |
| 94 | + // No ConfigureDbContext here on purpose. |
| 95 | + services.AddPooledDbContextFactory<TestDbContext>(); |
| 96 | + |
| 97 | + using var provider = services.BuildServiceProvider(); |
| 98 | + var factory = provider.GetRequiredService<IDbContextFactory<TestDbContext>>(); |
| 99 | + await using var db = await factory.CreateDbContextAsync(); |
| 100 | + |
| 101 | + // Trigger provider requirement (any DB operation works; EnsureCreated is simple & provider-agnostic) |
| 102 | + await Assert.ThrowsAsync<InvalidOperationException>(async () => |
| 103 | + { |
| 104 | + await db.Database.EnsureCreatedAsync(); |
| 105 | + }); |
| 106 | + } |
| 107 | +} |
0 commit comments