@@ -315,4 +315,51 @@ public void TestUseJsonConfigurationReturnDbConnectionStringProviderIsNotNull()
315
315
Assert . AreEqual ( 1 , dbConnectionStringProvider . DbContextOptionsList . Count ) ;
316
316
Assert . AreEqual ( connectionString , dbConnectionStringProvider . DbContextOptionsList [ 0 ] . ConnectionString ) ;
317
317
}
318
+
319
+ [ TestMethod ]
320
+ public async Task TestModifyConnectionString ( )
321
+ {
322
+ var services = new ServiceCollection ( ) ;
323
+ var configuration = new ConfigurationBuilder ( )
324
+ . AddJsonFile ( "appsettings.json" , true , true )
325
+ . Build ( ) ;
326
+ services . AddSingleton < IConfiguration > ( configuration ) ;
327
+ services . AddMasaDbContext < CustomQueryDbContext > ( optionsBuilder => optionsBuilder . UseSqlite ( ) ) ;
328
+
329
+ var serviceProvider = services . BuildServiceProvider ( ) ;
330
+ var connectionStringProvider = serviceProvider . GetService < IConnectionStringProvider > ( ) ;
331
+ Assert . IsNotNull ( connectionStringProvider ) ;
332
+
333
+ var connectionString = await connectionStringProvider . GetConnectionStringAsync ( ) ;
334
+ Assert . AreEqual ( "data source=test;" , connectionString ) ;
335
+
336
+ var rootPath = AppDomain . CurrentDomain . BaseDirectory ;
337
+
338
+ var expectedNewConnectionString = "data source=test2;" ;
339
+ var oldContent = await File . ReadAllTextAsync ( Path . Combine ( rootPath , "appsettings.json" ) ) ;
340
+ await File . WriteAllTextAsync ( Path . Combine ( rootPath , "appsettings.json" ) ,
341
+ System . Text . Json . JsonSerializer . Serialize ( new
342
+ {
343
+ ConnectionStrings = new
344
+ {
345
+ DefaultConnection = expectedNewConnectionString
346
+ }
347
+ } ) ) ;
348
+
349
+ await Task . Delay ( 2000 ) ;
350
+
351
+ connectionStringProvider = serviceProvider . GetService < IConnectionStringProvider > ( ) ;
352
+ Assert . IsNotNull ( connectionStringProvider ) ;
353
+
354
+ connectionString = await connectionStringProvider . GetConnectionStringAsync ( ) ;
355
+ Assert . AreEqual ( "data source=test;" , connectionString ) ;
356
+
357
+ connectionStringProvider = serviceProvider . CreateScope ( ) . ServiceProvider . GetService < IConnectionStringProvider > ( ) ;
358
+ Assert . IsNotNull ( connectionStringProvider ) ;
359
+
360
+ connectionString = await connectionStringProvider . GetConnectionStringAsync ( ) ;
361
+ Assert . AreEqual ( expectedNewConnectionString , connectionString ) ;
362
+
363
+ await File . WriteAllTextAsync ( Path . Combine ( rootPath , "appsettings.json" ) , oldContent ) ;
364
+ }
318
365
}
0 commit comments