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 ;
7
+ using JsonApiDotNetCore . Serialization . Response ;
5
8
using Microsoft . EntityFrameworkCore ;
6
9
using Microsoft . EntityFrameworkCore . Diagnostics ;
7
10
8
11
WebApplicationBuilder builder = WebApplication . CreateBuilder ( args ) ;
9
12
10
13
// Add services to the container.
11
14
12
- builder . Services . AddDbContext < SampleDbContext > ( options =>
15
+ builder . Services . AddDbContext < SqliteSampleDbContext > ( options =>
13
16
{
14
17
options . UseSqlite ( "Data Source=SampleDb.db;Pooling=False" ) ;
15
18
SetDbContextDebugOptions ( options ) ;
16
19
} ) ;
17
20
18
- builder . Services . AddJsonApi < SampleDbContext > ( options =>
21
+ builder . Services . AddDbContext < PostgreSqlSampleDbContext > ( options =>
22
+ {
23
+ options . UseNpgsql ( "Host=localhost;Database=ExampleDb;User ID=postgres;Password=postgres;Include Error Detail=true" ) ;
24
+ SetDbContextDebugOptions ( options ) ;
25
+ } ) ;
26
+
27
+ // EntityFrameworkCoreRepository injects IDbContextResolver to obtain the DbContext during a request.
28
+ builder . Services . AddHttpContextAccessor ( ) ;
29
+ builder . Services . AddScoped < IDbContextResolver , QueryStringDbContextResolver > ( ) ;
30
+
31
+ // Make rendered links contain the dbType query string parameter.
32
+ builder . Services . AddScoped < ILinkBuilder , DbAwareLinkBuilder > ( ) ;
33
+
34
+ // DbContext is used to scan the model at app startup. Pick any, since their entities are identical.
35
+ builder . Services . AddJsonApi < SqliteSampleDbContext > ( options =>
19
36
{
20
37
options . Namespace = "api" ;
21
38
options . UseRelativeLinks = true ;
22
39
options . IncludeTotalResourceCount = true ;
40
+ options . AllowUnknownQueryStringParameters = true ;
23
41
24
42
#if DEBUG
25
43
options . IncludeExceptionStackTraceInErrors = true ;
36
54
app . UseJsonApi ( ) ;
37
55
app . MapControllers ( ) ;
38
56
39
- await CreateDatabaseAsync ( app . Services ) ;
57
+ await CreateSqliteDatabaseAsync ( app . Services ) ;
58
+ await CreatePostgreSqlDatabaseAsync ( app . Services ) ;
40
59
41
60
app . Run ( ) ;
42
61
@@ -48,21 +67,19 @@ static void SetDbContextDebugOptions(DbContextOptionsBuilder options)
48
67
options . ConfigureWarnings ( builder => builder . Ignore ( CoreEventId . SensitiveDataLoggingEnabledWarning ) ) ;
49
68
}
50
69
51
- static async Task CreateDatabaseAsync ( IServiceProvider serviceProvider )
70
+ static async Task CreateSqliteDatabaseAsync ( IServiceProvider serviceProvider )
52
71
{
53
72
await using AsyncServiceScope scope = serviceProvider . CreateAsyncScope ( ) ;
54
73
55
- var dbContext = scope . ServiceProvider . GetRequiredService < SampleDbContext > ( ) ;
74
+ var dbContext = scope . ServiceProvider . GetRequiredService < SqliteSampleDbContext > ( ) ;
56
75
await dbContext . Database . EnsureDeletedAsync ( ) ;
57
76
await dbContext . Database . EnsureCreatedAsync ( ) ;
58
77
59
- await CreateSampleDataAsync ( dbContext ) ;
78
+ await CreateSqliteSampleDataAsync ( dbContext ) ;
60
79
}
61
80
62
- static async Task CreateSampleDataAsync ( SampleDbContext dbContext )
81
+ static async Task CreateSqliteSampleDataAsync ( SqliteSampleDbContext dbContext )
63
82
{
64
- // Note: The generate-examples.ps1 script (to create example requests in documentation) depends on these.
65
-
66
83
dbContext . Books . AddRange ( new Book
67
84
{
68
85
Title = "Frankenstein" ,
@@ -91,3 +108,37 @@ static async Task CreateSampleDataAsync(SampleDbContext dbContext)
91
108
92
109
await dbContext . SaveChangesAsync ( ) ;
93
110
}
111
+
112
+ static async Task CreatePostgreSqlDatabaseAsync ( IServiceProvider serviceProvider )
113
+ {
114
+ await using AsyncServiceScope scope = serviceProvider . CreateAsyncScope ( ) ;
115
+
116
+ var dbContext = scope . ServiceProvider . GetRequiredService < PostgreSqlSampleDbContext > ( ) ;
117
+ await dbContext . Database . EnsureDeletedAsync ( ) ;
118
+ await dbContext . Database . EnsureCreatedAsync ( ) ;
119
+
120
+ await CreatePostgreSqlSampleDataAsync ( dbContext ) ;
121
+ }
122
+
123
+ static async Task CreatePostgreSqlSampleDataAsync ( PostgreSqlSampleDbContext dbContext )
124
+ {
125
+ dbContext . Books . AddRange ( new Book
126
+ {
127
+ Title = "Wolf Hall" ,
128
+ PublishYear = 2009 ,
129
+ Author = new Person
130
+ {
131
+ Name = "Hilary Mantel"
132
+ }
133
+ } , new Book
134
+ {
135
+ Title = "Gilead" ,
136
+ PublishYear = 2004 ,
137
+ Author = new Person
138
+ {
139
+ Name = "Marilynne Robinson"
140
+ }
141
+ } ) ;
142
+
143
+ await dbContext . SaveChangesAsync ( ) ;
144
+ }
0 commit comments