---
title: Persist context rows
summary: Replaces an application's context rows for a session with the current in-memory context values, as a full delete-then-insert.
status: draft
owner: SAIF Doc Gen
last_reviewed: 2026-08-17
library: SAIF.Presentation.Web.MVC
reference_type: package
operation_id: OP-MVC-08
trigger: Called by Navigate to a view on a genuine cross-application transition
---

# Persist context rows

Writes the current in-memory context hashtable to the SQL Server `Context` table so the destination application can retrieve it. The write is a full replace, not a merge — any prior rows for the application/session are cleared first.

| Field | Value |
| --- | --- |
| **Operation ID** | OP-MVC-08 |
| **Trigger** | Called by [Navigate to a view](navigate-to-view.md) on a genuine cross-application transition |
| **Inputs** | Application name, session ID, in-memory context hashtable |
| **Outputs** | Prior rows deleted, new rows inserted |
| **Primary components** | `clsContext.SaveContext` |
| **Entry point** | SAIF.Presentation.Web.MVC/clsContext.vb |

## Steps

1. Create a `clsContextRepository` and call `Delete(applicationName, sessionID)` to clear prior rows for this app/session ([SQL-004](../sql/SQL-004-delete-context-rows.md), [BR-MVC-017](../business-rules.md#br-mvc-017-cross-application-context-is-consumed-exactly-once)).
2. Iterate every key in the in-memory `Hashtable` and call `Save(appName, sessionID, key, value)`, inserting one row per entry with `CURRENT_TIMESTAMP` as the update date ([SQL-001](../sql/SQL-001-insert-context-row.md), [Context](../database-tables.md#context)).
3. Each operation opens its own `SqlConnection` obtained from `clsConnection.getConnection(<NetFrameworkSqlServer setting>, "APPM", "NETFRAMEWORK", connection)` ([BR-MVC-019](../business-rules.md#br-mvc-019-context-persistence-targets-database-appm-under-tag-netframework)).
   - On failure: failures are wrapped as `ApplicationException("Context Repository Save Failed.", ex)`.

## Process flow

The following diagram visualizes this operation's control flow.

```mermaid
flowchart TD
	Start([SaveContext]) --> S1[DELETE existing rows for app/session]
	S1 --> S2{More keys in context hashtable?}
	S2 -->|Yes| S3[INSERT one row for the key]
	S3 --> S2
	S2 -->|No| EndOk([Context persisted])
	S1 -.->|Exception| EndErr([Wrap as ApplicationException])
	S3 -.->|Exception| EndErr
```

## Failure and edge-case behavior
- If the delete succeeds but a later insert fails partway through the key set, the application/session ends up with a partial context row set rather than the full replacement — no transaction wraps the delete-then-insert sequence.**
- Each context key/value pair opens its own `SqlConnection`, so a large context hashtable results in one connection open/close per key rather than a single batched write.*

## Related documentation
- [Operations and processes](../operations-processes.md)
- [Business rules](../business-rules.md)
- [SQL catalog](../sql/index.md)
- [Database tables](../database-tables.md)

> **Note:** Statements marked with `*` were inferred from code structure, naming conventions, or contextual evidence where direct confirmation was not available in source artifacts. Statements marked with `**` could not be confirmed; treat as unknown until verified.

## Sources
- SAIF.Presentation.Web.MVC/clsContext.vb
