|
| 1 | +using Microsoft.EntityFrameworkCore.Diagnostics; |
| 2 | +using Microsoft.EntityFrameworkCore.Infrastructure; |
| 3 | + |
| 4 | +namespace NewInEfCore7; |
| 5 | + |
| 6 | +public static class LazyConnectionStringSample |
| 7 | +{ |
| 8 | + public static async Task Lazy_initialization_of_a_connection_string() |
| 9 | + { |
| 10 | + PrintSampleName(); |
| 11 | + |
| 12 | + var services = new ServiceCollection(); |
| 13 | + |
| 14 | + services.AddScoped<IClientConnectionStringFactory, TestClientConnectionStringFactory>(); |
| 15 | + |
| 16 | + services.AddDbContext<CustomerContext>( |
| 17 | + b => b.UseSqlServer() |
| 18 | + .LogTo(Console.WriteLine, LogLevel.Information) |
| 19 | + .EnableSensitiveDataLogging()); |
| 20 | + |
| 21 | + var serviceProvider = services.BuildServiceProvider(); |
| 22 | + |
| 23 | + using (var scope = serviceProvider.CreateScope()) |
| 24 | + { |
| 25 | + var context = scope.ServiceProvider.GetRequiredService<CustomerContext>(); |
| 26 | + |
| 27 | + await context.Database.EnsureDeletedAsync(); |
| 28 | + await context.Database.EnsureCreatedAsync(); |
| 29 | + |
| 30 | + await context.AddRangeAsync( |
| 31 | + new Customer { Name = "Alice" }, |
| 32 | + new Customer { Name = "Mac" }); |
| 33 | + |
| 34 | + await context.SaveChangesAsync(); |
| 35 | + |
| 36 | + var customer = await context.Customers.SingleAsync(e => e.Name == "Alice"); |
| 37 | + Console.WriteLine(); |
| 38 | + Console.WriteLine($"Loaded {customer.Name}"); |
| 39 | + Console.WriteLine(); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + private static void PrintSampleName([CallerMemberName] string? methodName = null) |
| 44 | + { |
| 45 | + Console.WriteLine($">>>> Sample: {methodName}"); |
| 46 | + Console.WriteLine(); |
| 47 | + } |
| 48 | + |
| 49 | + public class CustomerContext : DbContext |
| 50 | + { |
| 51 | + private readonly IClientConnectionStringFactory _connectionStringFactory; |
| 52 | + |
| 53 | + public CustomerContext( |
| 54 | + DbContextOptions<CustomerContext> options, |
| 55 | + IClientConnectionStringFactory connectionStringFactory) |
| 56 | + : base(options) |
| 57 | + { |
| 58 | + _connectionStringFactory = connectionStringFactory; |
| 59 | + } |
| 60 | + |
| 61 | + public DbSet<Customer> Customers |
| 62 | + => Set<Customer>(); |
| 63 | + |
| 64 | + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) |
| 65 | + => optionsBuilder.AddInterceptors( |
| 66 | + new ConnectionStringInitializationInterceptor(_connectionStringFactory)); |
| 67 | + } |
| 68 | + |
| 69 | + public interface IClientConnectionStringFactory |
| 70 | + { |
| 71 | + Task<string> GetConnectionStringAsync(CancellationToken cancellationToken); |
| 72 | + } |
| 73 | + |
| 74 | + public class TestClientConnectionStringFactory : IClientConnectionStringFactory |
| 75 | + { |
| 76 | + public Task<string> GetConnectionStringAsync(CancellationToken cancellationToken) |
| 77 | + { |
| 78 | + Console.WriteLine(); |
| 79 | + Console.WriteLine(">>> Getting connection string..."); |
| 80 | + Console.WriteLine(); |
| 81 | + return Task.FromResult(@"Server=(localdb)\mssqllocaldb;Database=LazyConnectionStringSample"); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + #region ConnectionStringInitializationInterceptor |
| 86 | + public class ConnectionStringInitializationInterceptor : DbConnectionInterceptor |
| 87 | + { |
| 88 | + private readonly IClientConnectionStringFactory _connectionStringFactory; |
| 89 | + |
| 90 | + public ConnectionStringInitializationInterceptor(IClientConnectionStringFactory connectionStringFactory) |
| 91 | + { |
| 92 | + _connectionStringFactory = connectionStringFactory; |
| 93 | + } |
| 94 | + |
| 95 | + public override InterceptionResult ConnectionOpening( |
| 96 | + DbConnection connection, |
| 97 | + ConnectionEventData eventData, |
| 98 | + InterceptionResult result) |
| 99 | + => throw new NotSupportedException("Synchronous connections not supported."); |
| 100 | + |
| 101 | + public override async ValueTask<InterceptionResult> ConnectionOpeningAsync( |
| 102 | + DbConnection connection, ConnectionEventData eventData, InterceptionResult result, |
| 103 | + CancellationToken cancellationToken = new()) |
| 104 | + { |
| 105 | + if (string.IsNullOrEmpty(connection.ConnectionString)) |
| 106 | + { |
| 107 | + connection.ConnectionString = (await _connectionStringFactory.GetConnectionStringAsync(cancellationToken)); |
| 108 | + } |
| 109 | + |
| 110 | + return result; |
| 111 | + } |
| 112 | + } |
| 113 | + #endregion |
| 114 | + |
| 115 | + public class Customer |
| 116 | + { |
| 117 | + public int Id { get; set; } |
| 118 | + public string Name { get; set; } = null!; |
| 119 | + } |
| 120 | +} |
0 commit comments