22using AutoMapper ;
33using GraphQL . AspNet . Configuration . Mvc ;
44using Microsoft . AspNetCore . Authentication . JwtBearer ;
5+ using Microsoft . Data . Sqlite ;
6+ using Microsoft . EntityFrameworkCore ;
7+ using Microsoft . EntityFrameworkCore . Diagnostics ;
8+ using Microsoft . Extensions . Options ;
59using Microsoft . IdentityModel . Tokens ;
610using Microsoft . OpenApi . Models ;
7- using Newtonsoft . Json ;
811using Newtonsoft . Json . Converters ;
912using NorthwindCRUD . Filters ;
1013using NorthwindCRUD . Helpers ;
11- using NorthwindCRUD . Middlewares ;
12- using NorthwindCRUD . Providers ;
1314using NorthwindCRUD . Services ;
1415
1516namespace NorthwindCRUD
@@ -37,8 +38,7 @@ public static void Main(string[] args)
3738 options . SuppressImplicitRequiredAttributeForNonNullableReferenceTypes = true ;
3839 } ) . AddNewtonsoftJson ( options =>
3940 {
40- options . SerializerSettings . DateParseHandling = DateParseHandling . None ;
41- options . SerializerSettings . ReferenceLoopHandling = ReferenceLoopHandling . Ignore ;
41+ options . SerializerSettings . ReferenceLoopHandling = Newtonsoft . Json . ReferenceLoopHandling . Ignore ;
4242 options . SerializerSettings . Converters . Add ( new StringEnumConverter ( ) ) ;
4343 } ) ;
4444
@@ -57,7 +57,6 @@ public static void Main(string[] args)
5757 Scheme = "bearer" ,
5858 } ) ;
5959
60- option . SchemaFilter < EnumSchemaFilter > ( ) ;
6160 option . SchemaFilter < EnumSchemaFilter > ( ) ;
6261 option . OperationFilter < AuthResponsesOperationFilter > ( ) ;
6362 option . EnableAnnotations ( ) ;
@@ -75,10 +74,36 @@ public static void Main(string[] args)
7574 } ) ;
7675 } ) ;
7776
78- builder . Services . AddDbContext < DataContext > ( ( serviceProvider , options ) =>
77+ var dbProvider = builder . Configuration . GetConnectionString ( "Provider" ) ;
78+
79+ if ( dbProvider == "SQLite" )
7980 {
80- var configurationProvider = serviceProvider . GetRequiredService < DbContextConfigurationProvider > ( ) ;
81- configurationProvider . ConfigureOptions ( options ) ;
81+ // For SQLite in memory to be shared across multiple EF calls, we need to maintain a separate open connection.
82+ // see post https://stackoverflow.com/questions/56319638/entityframeworkcore-sqlite-in-memory-db-tables-are-not-created
83+ var keepAliveConnection = new SqliteConnection ( builder . Configuration . GetConnectionString ( "SQLiteConnectionString" ) ) ;
84+ keepAliveConnection . Open ( ) ;
85+ }
86+
87+ builder . Services . AddDbContext < DataContext > ( options =>
88+ {
89+ if ( dbProvider == "SqlServer" )
90+ {
91+ options . UseSqlServer ( builder . Configuration . GetConnectionString ( "SqlServerConnectionString" ) ) ;
92+ }
93+ else if ( dbProvider == "InMemory" )
94+ {
95+ options . ConfigureWarnings ( warnOpts =>
96+ {
97+ // InMemory doesn't support transactions and we're ok with it
98+ warnOpts . Ignore ( InMemoryEventId . TransactionIgnoredWarning ) ;
99+ } ) ;
100+
101+ options . UseInMemoryDatabase ( databaseName : builder . Configuration . GetConnectionString ( "InMemoryDBConnectionString" ) ) ;
102+ }
103+ else if ( dbProvider == "SQLite" )
104+ {
105+ options . UseSqlite ( builder . Configuration . GetConnectionString ( "SQLiteConnectionString" ) ) ;
106+ }
82107 } ) ;
83108
84109 var config = new MapperConfiguration ( cfg =>
@@ -110,10 +135,8 @@ public static void Main(string[] args)
110135 } ) ;
111136
112137 builder . Services . AddAuthorization ( ) ;
113- builder . Services . AddHttpContextAccessor ( ) ;
114- builder . Services . AddMemoryCache ( ) ;
138+
115139 builder . Services . AddScoped < DBSeeder > ( ) ;
116- builder . Services . AddScoped < DbContextConfigurationProvider > ( ) ;
117140 builder . Services . AddTransient < CategoryService > ( ) ;
118141 builder . Services . AddTransient < CustomerService > ( ) ;
119142 builder . Services . AddTransient < EmployeeTerritoryService > ( ) ;
@@ -132,7 +155,7 @@ public static void Main(string[] args)
132155
133156 // Necessary to detect if it's behind a load balancer, for example changing protocol, port or hostname
134157 app . UseForwardedHeaders ( ) ;
135- app . UseMiddleware < TenantHeaderValidationMiddleware > ( ) ;
158+
136159 app . UseHttpsRedirection ( ) ;
137160 app . UseDefaultFiles ( ) ;
138161 app . UseStaticFiles ( ) ;
@@ -163,4 +186,4 @@ public static void Main(string[] args)
163186 app . Run ( ) ;
164187 }
165188 }
166- }
189+ }
0 commit comments