11using Microsoft . EntityFrameworkCore . Infrastructure ;
22using Microsoft . Extensions . DependencyInjection ;
3+ using Thinktecture . EntityFrameworkCore . Testing ;
34using Thinktecture . TestDatabaseContext ;
45
56namespace Thinktecture . EntityFrameworkCore . Infrastructure . DefaultSchemaModelCustomizerTests ;
67
78public class Customize : IntegrationTestsBase
89{
10+ private string ? _schema ;
11+
912 public Customize ( ITestOutputHelper testOutputHelper )
1013 : base ( testOutputHelper )
1114 {
12- ConfigureOptionsBuilder = builder =>
13- {
14- builder . AddOrUpdateExtension < RelationalDbContextOptionsExtension > ( extension =>
15- {
16- extension . Register ( typeof ( ModelCustomizer ) , typeof ( ModelCustomizer ) , ServiceLifetime . Singleton ) ;
17- return extension ;
18- } ) ;
19- builder . ReplaceService < IModelCustomizer , DefaultSchemaModelCustomizer < ModelCustomizer > > ( ) ;
20- } ;
15+ }
16+
17+ protected override void ConfigureTestDbContextProvider ( SqliteTestDbContextProviderBuilder < DbContextWithSchema > builder )
18+ {
19+ base . ConfigureTestDbContextProvider ( builder ) ;
20+
21+ builder . ConfigureOptions ( optionsBuilder =>
22+ {
23+ optionsBuilder . AddOrUpdateExtension < RelationalDbContextOptionsExtension > ( extension =>
24+ {
25+ extension . Register ( typeof ( ModelCustomizer ) , typeof ( ModelCustomizer ) , ServiceLifetime . Singleton ) ;
26+ return extension ;
27+ } ) ;
28+ optionsBuilder . ReplaceService < IModelCustomizer , DefaultSchemaModelCustomizer < ModelCustomizer > > ( ) ;
29+ } ) ;
30+
31+ if ( _schema is not null )
32+ {
33+ builder . UseContextFactory ( options => new DbContextWithSchema ( options , _schema ) )
34+ . DisableModelCache ( ) ;
35+ }
2136 }
2237
2338 [ Fact ]
2439 public void Should_set_schema_if_entity_schema_is_null ( )
2540 {
26- Schema = "BA3C32B0-D7EC-422C-AE3B-3206E7D67735" ;
41+ _schema = "BA3C32B0-D7EC-422C-AE3B-3206E7D67735" ;
2742
28- ActDbContext . Model . FindEntityType ( typeof ( TestEntity ) ) ! . GetSchema ( ) . Should ( ) . Be ( Schema ) ;
43+ ActDbContext . Model . FindEntityType ( typeof ( TestEntity ) ) ! . GetSchema ( ) . Should ( ) . Be ( _schema ) ;
2944 }
3045
3146 [ Fact ]
3247 public void Should_set_schema_if_view_schema_is_null ( )
3348 {
34- Schema = "E2FBA720-E24C-46C9-B326-46C3C91707F5" ;
49+ _schema = "E2FBA720-E24C-46C9-B326-46C3C91707F5" ;
3550
36- ActDbContext . Model . FindEntityType ( typeof ( TestQuery ) ) ! . GetViewSchema ( ) . Should ( ) . Be ( Schema ) ;
51+ ActDbContext . Model . FindEntityType ( typeof ( TestQuery ) ) ! . GetViewSchema ( ) . Should ( ) . Be ( _schema ) ;
3752 }
3853
3954 [ Fact ]
4055 public void Should_set_schema_if_dbFunction_schema_is_null ( )
4156 {
42- Schema = "E2FBA720-E24C-46C9-B326-46C3C91707F5" ;
57+ _schema = "E2FBA720-E24C-46C9-B326-46C3C91707F5" ;
4358
4459 ActDbContext . Model . GetDbFunctions ( )
4560 . Single ( f => f . MethodInfo ! . Name == nameof ( DbContextWithSchema . TestDbFunction ) )
46- . Schema . Should ( ) . Be ( Schema ) ;
61+ . Schema . Should ( ) . Be ( _schema ) ;
4762 }
4863
4964 [ Fact ]
5065 public void Should_not_change_schema_if_entity_schema_is_set_already ( )
5166 {
52- Schema = "4BA05B95-7FEA-4F32-A1E0-ACA9CB486EB9" ;
67+ _schema = "4BA05B95-7FEA-4F32-A1E0-ACA9CB486EB9" ;
5368 ConfigureModel = builder => builder . Entity < TestEntity > ( ) . ToTable ( "Table" , "Schema" ) ;
5469
5570 ActDbContext . Model . FindEntityType ( typeof ( TestEntity ) ) ! . GetSchema ( ) . Should ( ) . Be ( "Schema" ) ;
@@ -58,7 +73,7 @@ public void Should_not_change_schema_if_entity_schema_is_set_already()
5873 [ Fact ]
5974 public void Should_not_reset_schema_if_entity_schema_is_set_already ( )
6075 {
61- Schema = null ;
76+ _schema = null ;
6277 ConfigureModel = builder => builder . Entity < TestEntity > ( ) . ToTable ( "Table" , "Schema" ) ;
6378
6479 ActDbContext . Model . FindEntityType ( typeof ( TestEntity ) ) ! . GetSchema ( ) . Should ( ) . Be ( "Schema" ) ;
0 commit comments