11using System . Diagnostics ;
2+ using GettingStarted ;
23using GettingStarted . Data ;
34using GettingStarted . Models ;
45using JsonApiDotNetCore . Configuration ;
6+ using JsonApiDotNetCore . Repositories ;
57using Microsoft . EntityFrameworkCore ;
68using Microsoft . EntityFrameworkCore . Diagnostics ;
79
810WebApplicationBuilder builder = WebApplication . CreateBuilder ( args ) ;
911
1012// Add services to the container.
1113
12- builder . Services . AddDbContext < SampleDbContext > ( options =>
14+ builder . Services . AddDbContext < SqliteSampleDbContext > ( options =>
1315{
1416 options . UseSqlite ( "Data Source=SampleDb.db;Pooling=False" ) ;
1517 SetDbContextDebugOptions ( options ) ;
1618} ) ;
1719
18- builder . Services . AddJsonApi < SampleDbContext > ( options =>
20+ builder . Services . AddDbContext < PostgreSqlSampleDbContext > ( options =>
21+ {
22+ options . UseNpgsql ( "Host=localhost;Database=ExampleDb;User ID=postgres;Password=postgres;Include Error Detail=true" ) ;
23+ SetDbContextDebugOptions ( options ) ;
24+ } ) ;
25+
26+ // EntityFrameworkCoreRepository injects IDbContextResolver to obtain the DbContext during a request.
27+ builder . Services . AddHttpContextAccessor ( ) ;
28+ builder . Services . AddScoped < IDbContextResolver , QueryStringDbContextResolver > ( ) ;
29+
30+ // DbContext is used to scan the model at app startup. Pick any, since their entities are identical.
31+ builder . Services . AddJsonApi < SqliteSampleDbContext > ( options =>
1932{
2033 options . Namespace = "api" ;
2134 options . UseRelativeLinks = true ;
2235 options . IncludeTotalResourceCount = true ;
36+ options . AllowUnknownQueryStringParameters = true ;
2337
2438#if DEBUG
2539 options . IncludeExceptionStackTraceInErrors = true ;
3650app . UseJsonApi ( ) ;
3751app . MapControllers ( ) ;
3852
39- await CreateDatabaseAsync ( app . Services ) ;
53+ await CreateSqliteDatabaseAsync ( app . Services ) ;
54+ await CreatePostgreSqlDatabaseAsync ( app . Services ) ;
4055
4156app . Run ( ) ;
4257
@@ -48,21 +63,19 @@ static void SetDbContextDebugOptions(DbContextOptionsBuilder options)
4863 options . ConfigureWarnings ( builder => builder . Ignore ( CoreEventId . SensitiveDataLoggingEnabledWarning ) ) ;
4964}
5065
51- static async Task CreateDatabaseAsync ( IServiceProvider serviceProvider )
66+ static async Task CreateSqliteDatabaseAsync ( IServiceProvider serviceProvider )
5267{
5368 await using AsyncServiceScope scope = serviceProvider . CreateAsyncScope ( ) ;
5469
55- var dbContext = scope . ServiceProvider . GetRequiredService < SampleDbContext > ( ) ;
70+ var dbContext = scope . ServiceProvider . GetRequiredService < SqliteSampleDbContext > ( ) ;
5671 await dbContext . Database . EnsureDeletedAsync ( ) ;
5772 await dbContext . Database . EnsureCreatedAsync ( ) ;
5873
59- await CreateSampleDataAsync ( dbContext ) ;
74+ await CreateSqliteSampleDataAsync ( dbContext ) ;
6075}
6176
62- static async Task CreateSampleDataAsync ( SampleDbContext dbContext )
77+ static async Task CreateSqliteSampleDataAsync ( SqliteSampleDbContext dbContext )
6378{
64- // Note: The generate-examples.ps1 script (to create example requests in documentation) depends on these.
65-
6679 dbContext . Books . AddRange ( new Book
6780 {
6881 Title = "Frankenstein" ,
@@ -91,3 +104,37 @@ static async Task CreateSampleDataAsync(SampleDbContext dbContext)
91104
92105 await dbContext . SaveChangesAsync ( ) ;
93106}
107+
108+ static async Task CreatePostgreSqlDatabaseAsync ( IServiceProvider serviceProvider )
109+ {
110+ await using AsyncServiceScope scope = serviceProvider . CreateAsyncScope ( ) ;
111+
112+ var dbContext = scope . ServiceProvider . GetRequiredService < PostgreSqlSampleDbContext > ( ) ;
113+ await dbContext . Database . EnsureDeletedAsync ( ) ;
114+ await dbContext . Database . EnsureCreatedAsync ( ) ;
115+
116+ await CreatePostgreSqlSampleDataAsync ( dbContext ) ;
117+ }
118+
119+ static async Task CreatePostgreSqlSampleDataAsync ( PostgreSqlSampleDbContext dbContext )
120+ {
121+ dbContext . Books . AddRange ( new Book
122+ {
123+ Title = "Wolf Hall" ,
124+ PublishYear = 2009 ,
125+ Author = new Person
126+ {
127+ Name = "Hilary Mantel"
128+ }
129+ } , new Book
130+ {
131+ Title = "Gilead" ,
132+ PublishYear = 2004 ,
133+ Author = new Person
134+ {
135+ Name = "Marilynne Robinson"
136+ }
137+ } ) ;
138+
139+ await dbContext . SaveChangesAsync ( ) ;
140+ }
0 commit comments