Skip to content

Commit 0874c49

Browse files
committed
BFF upgrade guides - add EF Core migrations
1 parent 904ec75 commit 0874c49

File tree

3 files changed

+63
-10
lines changed

3 files changed

+63
-10
lines changed

src/content/docs/bff/fundamentals/session/server-side-sessions.mdx

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ The default implementation stores the session in-memory. This is useful for test
3434

3535
## Using Entity Framework for the Server-side Session Store
3636

37-
To use the EF session store, install the *Duende.BFF.EntityFramework* NuGet package and register it by calling *AddEntityFrameworkServerSideSessions*, like this:
37+
To use the EF session store, install the `Duende.BFF.EntityFramework` NuGet package and register it by calling `AddEntityFrameworkServerSideSessions`, like this:
3838

3939
```csharp
4040
var cn = _configuration.GetConnectionString("db");
@@ -67,14 +67,14 @@ Note, you'll still need to let the server side session store know about the `Ses
6767

6868
### Entity Framework Migrations
6969

70-
Most data stores that you might use with Entity Framework use a schema to define the structure of their data. *Duende.BFF.EntityFramework* doesn't make any assumptions about the underlying datastore, how (or indeed even if) it defines its schema, or how schema changes are managed by your organization. For these reasons, Duende does not directly support database creation, schema changes, or data migration by publishing database scripts.
70+
Most data stores that you might use with Entity Framework use a schema to define the structure of their data. `Duende.BFF.EntityFramework` doesn't make any assumptions about the underlying datastore, how (or indeed even if) it defines its schema, or how schema changes are managed by your organization. For these reasons, Duende does not directly support database creation, schema changes, or data migration by publishing database scripts.
7171

72-
You are expected to manage your database in the way your organization sees fit. Using EF migrations is one possible approach to that, which Duende facilitates by publishing entity classes in each version of *Duende.BFF.EntityFramework*. An example project that uses those entities to create migrations is [here](https://github.com/DuendeSoftware/products/tree/main/bff/migrations/UserSessionDb).
72+
You are expected to manage your database in the way your organization sees fit. Using EF migrations is one possible approach to that, which Duende facilitates by publishing entity classes in each version of `Duende.BFF.EntityFramework`. An example project that uses those entities to create migrations is [here](https://github.com/DuendeSoftware/products/tree/main/bff/migrations/UserSessionDb).
7373

7474
To quickly create Entity Framework migrations, run the following command in the project directory that has access to Entity Framework Core's tools:
7575

7676
```bash
77-
dotnet ef migrations add UserSessions -o Migrations
77+
dotnet ef migrations add UserSessions -o Migrations -c SessionDbContext
7878
```
7979

8080
The project must also reference the `Duende.BFF.EntityFramework` NuGet package and the `Microsoft.EntityFrameworkCore.Design` NuGet package, along with a specific database provider and its corresponding configuration, including the connection string.
@@ -89,7 +89,7 @@ Abandoned sessions will remain in the store unless something removes the stale e
8989
{/* prettier-ignore */}
9090
<TabItem label="V4">
9191

92-
If you wish to have such sessions cleaned up periodically, then you can add the session cleanup host and configure the *SessionCleanupInterval* options:
92+
If you wish to have such sessions cleaned up periodically, then you can add the session cleanup host and configure the `SessionCleanupInterval` options:
9393

9494
<Code
9595
lang="csharp"
@@ -99,9 +99,9 @@ Abandoned sessions will remain in the store unless something removes the stale e
9999
})
100100
.AddServerSideSessions();`}/>
101101

102-
This requires an implementation of [*IUserSessionStoreCleanup*](/bff/extensibility/sessions#user-session-store-cleanup) in the ASP.NET Core service provider.
102+
This requires an implementation of [`IUserSessionStoreCleanup`](/bff/extensibility/sessions#user-session-store-cleanup) in the ASP.NET Core service provider.
103103

104-
If using Entity Framework Core, then the *IUserSessionStoreCleanup* implementation is provided for you when you use *AddEntityFrameworkServerSideSessions*.
104+
If using Entity Framework Core, then the `IUserSessionStoreCleanup` implementation is provided for you when you use `AddEntityFrameworkServerSideSessions`.
105105
You can then add the `SessionCleanupBackgroundProcess`:
106106

107107
<Code
@@ -125,7 +125,7 @@ builder.Services.AddBff()
125125
</TabItem>
126126
<TabItem label="V3">
127127

128-
If you wish to have such sessions cleaned up periodically, then you can configure the *EnableSessionCleanup* and *SessionCleanupInterval* options:
128+
If you wish to have such sessions cleaned up periodically, then you can configure the `EnableSessionCleanup` and `SessionCleanupInterval` options:
129129

130130
<Code
131131
lang="csharp"
@@ -136,9 +136,9 @@ builder.Services.AddBff()
136136
})
137137
.AddServerSideSessions();`}/>
138138

139-
This requires an implementation of [*IUserSessionStoreCleanup*](/bff/extensibility/sessions#user-session-store-cleanup) in the ASP.NET Core service provider.
139+
This requires an implementation of [`IUserSessionStoreCleanup`](/bff/extensibility/sessions#user-session-store-cleanup) in the ASP.NET Core service provider.
140140

141-
If using Entity Framework Core, then the *IUserSessionStoreCleanup* implementation is provided for you when you use *AddEntityFrameworkServerSideSessions*.
141+
If using Entity Framework Core, then the `IUserSessionStoreCleanup` implementation is provided for you when you use `AddEntityFrameworkServerSideSessions`.
142142
Just enable session cleanup:
143143

144144
<Code

src/content/docs/bff/upgrading/bff-v2-to-v3.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,4 +137,31 @@ If you used `BffBlazorOptions.StateProviderPollingInterval` or `BffBlazorOptions
137137
different polling settings, you should now consider if this same setting applies to either Server, WASM or both. Set the
138138
appropriate properties accordingly.
139139

140+
### Server Side Sessions Database Migrations
140141

142+
No [Entity Framework database migrations](/bff/fundamentals/session/server-side-sessions.mdx#entity-framework-migrations) are required for the server side sessions feature when using the `Duende.BFF.EntityFramework` package.
143+
144+
The database structure remains the same:
145+
146+
```sqlite
147+
// serversidesessions.sql
148+
CREATE TABLE "UserSessions" (
149+
"Id" INTEGER NOT NULL CONSTRAINT "PK_UserSessions" PRIMARY KEY AUTOINCREMENT,
150+
"ApplicationName" TEXT NULL,
151+
"SubjectId" TEXT NOT NULL,
152+
"SessionId" TEXT NULL,
153+
"Created" TEXT NOT NULL,
154+
"Renewed" TEXT NOT NULL,
155+
"Expires" TEXT NULL,
156+
"Ticket" TEXT NOT NULL,
157+
"Key" TEXT NOT NULL
158+
);
159+
160+
CREATE UNIQUE INDEX "IX_UserSessions_ApplicationName_Key" ON "UserSessions" ("ApplicationName", "Key");
161+
162+
CREATE UNIQUE INDEX "IX_UserSessions_ApplicationName_SessionId" ON "UserSessions" ("ApplicationName", "SessionId");
163+
164+
CREATE UNIQUE INDEX "IX_UserSessions_ApplicationName_SubjectId_SessionId" ON "UserSessions" ("ApplicationName", "SubjectId", "SessionId");
165+
166+
CREATE INDEX "IX_UserSessions_Expires" ON "UserSessions" ("Expires");
167+
```

src/content/docs/bff/upgrading/bff-v3-to-v4.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,4 +129,30 @@ You can configure the location of your `index.html` by specifying:
129129

130130
```csharp
131131
.WithIndexHtmlUrl(new Uri("https://localhost:5005/static/index.html"))
132+
```
133+
134+
### Server Side Sessions Database Migrations
135+
136+
When using the server side sessions feature backed by the `Duende.BFF.EntityFramework` package, you will need to script [Entity Framework database migrations](/bff/fundamentals/session/server-side-sessions.mdx#entity-framework-migrations) and apply these changes to your database.
137+
138+
```shell
139+
dotnet ef migrations add BFFUserSessionsV4 -o Migrations -c SessionDbContext
140+
```
141+
142+
In the `UserSessions` table, a number of changes were introduced:
143+
* The `ApplicationName` column was renamed to `PartitionKey`.
144+
* Related indexes were updated.
145+
146+
```sqlite
147+
// serversidesessions.sql
148+
ALTER TABLE "UserSessions" RENAME COLUMN "ApplicationName" TO "PartitionKey";
149+
150+
DROP INDEX "IX_UserSessions_ApplicationName_SubjectId_SessionId";
151+
CREATE UNIQUE INDEX "IX_UserSessions_PartitionKey_SubjectId_SessionId" ON "UserSessions" ("PartitionKey", "SubjectId", "SessionId");
152+
153+
DROP INDEX "IX_UserSessions_ApplicationName_SessionId";
154+
CREATE UNIQUE INDEX "IX_UserSessions_PartitionKey_SessionId" ON "UserSessions" ("PartitionKey", "SessionId");
155+
156+
DROP INDEX "IX_UserSessions_ApplicationName_Key";
157+
CREATE UNIQUE INDEX "IX_UserSessions_PartitionKey_Key" ON "UserSessions" ("PartitionKey", "Key");
132158
```

0 commit comments

Comments
 (0)