1+ using System . Diagnostics ;
12using System . Diagnostics . CodeAnalysis ;
23using System . Text . Json . Serialization ;
34using JsonApiDotNetCore . Configuration ;
45using JsonApiDotNetCore . Diagnostics ;
56using JsonApiDotNetCoreExample . Data ;
67using Microsoft . AspNetCore . Authentication ;
78using Microsoft . EntityFrameworkCore ;
9+ using Microsoft . EntityFrameworkCore . Diagnostics ;
810using Microsoft . Extensions . DependencyInjection . Extensions ;
911
1012[ assembly: ExcludeFromCodeCoverage ]
@@ -50,10 +52,7 @@ static void ConfigureServices(WebApplicationBuilder builder)
5052 string ? connectionString = GetConnectionString ( builder . Configuration ) ;
5153 options . UseNpgsql ( connectionString ) ;
5254
53- #if DEBUG
54- options . EnableSensitiveDataLogging ( ) ;
55- options . EnableDetailedErrors ( ) ;
56- #endif
55+ SetDbContextDebugOptions ( options ) ;
5756 } ) ;
5857
5958 using ( CodeTimingSessionManager . Current . Measure ( "AddJsonApi()" ) )
@@ -63,12 +62,12 @@ static void ConfigureServices(WebApplicationBuilder builder)
6362 options . Namespace = "api" ;
6463 options . UseRelativeLinks = true ;
6564 options . IncludeTotalResourceCount = true ;
66- options . SerializerOptions . WriteIndented = true ;
6765 options . SerializerOptions . Converters . Add ( new JsonStringEnumConverter ( ) ) ;
6866
6967#if DEBUG
7068 options . IncludeExceptionStackTraceInErrors = true ;
7169 options . IncludeRequestBodyInErrors = true ;
70+ options . SerializerOptions . WriteIndented = true ;
7271#endif
7372 } , discovery => discovery . AddCurrentAssembly ( ) ) ;
7473 }
@@ -80,6 +79,14 @@ static void ConfigureServices(WebApplicationBuilder builder)
8079 return configuration . GetConnectionString ( "Default" ) ? . Replace ( "###" , postgresPassword ) ;
8180}
8281
82+ [ Conditional ( "DEBUG" ) ]
83+ static void SetDbContextDebugOptions ( DbContextOptionsBuilder options )
84+ {
85+ options . EnableDetailedErrors ( ) ;
86+ options . EnableSensitiveDataLogging ( ) ;
87+ options . ConfigureWarnings ( builder => builder . Ignore ( CoreEventId . SensitiveDataLoggingEnabledWarning ) ) ;
88+ }
89+
8390static void ConfigurePipeline ( WebApplication webApplication )
8491{
8592 using IDisposable _ = CodeTimingSessionManager . Current . Measure ( "Configure pipeline" ) ;
@@ -99,8 +106,9 @@ static async Task CreateDatabaseAsync(IServiceProvider serviceProvider)
99106 await using AsyncServiceScope scope = serviceProvider . CreateAsyncScope ( ) ;
100107
101108 var dbContext = scope . ServiceProvider . GetRequiredService < AppDbContext > ( ) ;
102- await dbContext . Database . EnsureDeletedAsync ( ) ;
103- await dbContext . Database . EnsureCreatedAsync ( ) ;
104109
105- await Seeder . CreateSampleDataAsync ( dbContext ) ;
110+ if ( await dbContext . Database . EnsureCreatedAsync ( ) )
111+ {
112+ await Seeder . CreateSampleDataAsync ( dbContext ) ;
113+ }
106114}
0 commit comments