1
1
using System . Diagnostics ;
2
+ using GettingStarted ;
2
3
using GettingStarted . Data ;
3
4
using GettingStarted . Models ;
4
5
using JsonApiDotNetCore . Configuration ;
6
+ using JsonApiDotNetCore . Repositories ;
5
7
using Microsoft . EntityFrameworkCore ;
6
8
using Microsoft . EntityFrameworkCore . Diagnostics ;
7
9
8
10
WebApplicationBuilder builder = WebApplication . CreateBuilder ( args ) ;
9
11
10
12
// Add services to the container.
11
13
12
- builder . Services . AddDbContext < SampleDbContext > ( options =>
14
+ builder . Services . AddDbContext < SqliteSampleDbContext > ( options =>
13
15
{
14
16
options . UseSqlite ( "Data Source=SampleDb.db;Pooling=False" ) ;
15
17
SetDbContextDebugOptions ( options ) ;
16
18
} ) ;
17
19
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 =>
19
32
{
20
33
options . Namespace = "api" ;
21
34
options . UseRelativeLinks = true ;
22
35
options . IncludeTotalResourceCount = true ;
36
+ options . AllowUnknownQueryStringParameters = true ;
23
37
24
38
#if DEBUG
25
39
options . IncludeExceptionStackTraceInErrors = true ;
36
50
app . UseJsonApi ( ) ;
37
51
app . MapControllers ( ) ;
38
52
39
- await CreateDatabaseAsync ( app . Services ) ;
53
+ await CreateSqliteDatabaseAsync ( app . Services ) ;
54
+ await CreatePostgreSqlDatabaseAsync ( app . Services ) ;
40
55
41
56
app . Run ( ) ;
42
57
@@ -48,21 +63,19 @@ static void SetDbContextDebugOptions(DbContextOptionsBuilder options)
48
63
options . ConfigureWarnings ( builder => builder . Ignore ( CoreEventId . SensitiveDataLoggingEnabledWarning ) ) ;
49
64
}
50
65
51
- static async Task CreateDatabaseAsync ( IServiceProvider serviceProvider )
66
+ static async Task CreateSqliteDatabaseAsync ( IServiceProvider serviceProvider )
52
67
{
53
68
await using AsyncServiceScope scope = serviceProvider . CreateAsyncScope ( ) ;
54
69
55
- var dbContext = scope . ServiceProvider . GetRequiredService < SampleDbContext > ( ) ;
70
+ var dbContext = scope . ServiceProvider . GetRequiredService < SqliteSampleDbContext > ( ) ;
56
71
await dbContext . Database . EnsureDeletedAsync ( ) ;
57
72
await dbContext . Database . EnsureCreatedAsync ( ) ;
58
73
59
- await CreateSampleDataAsync ( dbContext ) ;
74
+ await CreateSqliteSampleDataAsync ( dbContext ) ;
60
75
}
61
76
62
- static async Task CreateSampleDataAsync ( SampleDbContext dbContext )
77
+ static async Task CreateSqliteSampleDataAsync ( SqliteSampleDbContext dbContext )
63
78
{
64
- // Note: The generate-examples.ps1 script (to create example requests in documentation) depends on these.
65
-
66
79
dbContext . Books . AddRange ( new Book
67
80
{
68
81
Title = "Frankenstein" ,
@@ -91,3 +104,37 @@ static async Task CreateSampleDataAsync(SampleDbContext dbContext)
91
104
92
105
await dbContext . SaveChangesAsync ( ) ;
93
106
}
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