# AppDbContext

- File ID: file:1pacvtg:appdbcontext-cs
- Path: NexusReader.Data/Persistence/AppDbContext.cs
- Role: database-config
- Layer: data

## Purpose

Defines the Entity Framework Core database context for the application: DbSet surface, EF configuration, model mapping (indexes, FKs, delete behavior), provider-specific type mapping, and deterministic seed data for subscription plans.

## Key Details

Important runtime behaviors: suppresses PendingModelChangesWarning (OnConfiguring), conditional provider-specific mapping for vector embeddings and HNSW index when PostgreSQL is used (lines 58-63), and seeds SubscriptionPlan rows with fixed IDs (lines 121-127) to ensure deterministic plan IDs.

## Symbols

- AppDbContext: Defines the Entity Framework Core database context for the application: DbSet surface, EF configuration, model mapping (indexes, FKs, delete behavior), provider-specific type mapping, and deterministic seed data for subscription plans.
- AppDbContext(): Constructs the EF DbContext with provided options.
- OnConfiguring(): Performs additional EF configuration; suppresses a pending-model-changes warning to avoid runtime exceptions in some environments.
- SemanticKnowledgeCache (DbSet property): Exposes the DbSet for SemanticKnowledgeCache entities.
- KnowledgeUnits (DbSet property): Exposes the DbSet for KnowledgeUnit entities.
- KnowledgeUnitLinks (DbSet property): Exposes the DbSet for KnowledgeUnitLink entities.
- Ebooks (DbSet property): Exposes the DbSet for Ebook entities.
- QuizResults (DbSet property): Exposes the DbSet for QuizResult entities.
- SubscriptionPlans (DbSet property): Exposes the DbSet for SubscriptionPlan entities.
- Authors (DbSet property): Exposes the DbSet for Author entities.
- OnModelCreating(): Configures EF model mappings: entity properties, indexes, relationships, provider-specific column types, and seeds subscription plans with deterministic IDs.
- AppDbContext: EF Core DbContext for application persistence, extending IdentityDbContext for user identity and domain entities.

## Dependencies

- No resolved dependencies.

## Calls

- No resolved calls.

## Unresolved References

- dependency-target: NexusReader.Domain/Entities (referenced via using NexusReader.Domain.Entities at line 3)
- dependency-target: Microsoft.EntityFrameworkCore (EF Core usage evident)

## Coverage

- Symbols: 12
- Executable symbols: 10
- Executable symbols with control flow: 10
- Grounded edges: 12
- Unresolved references: 2
- Control-flow fragments: 10
- Unresolved jumps: 0
- Unsupported constructs: 0
- Control-flow coverage: 100%
- Required diagrams: 2
- Generated diagrams: 3

## Control Flow Fragments

- AppDbContext: 3 nodes, 2 edges, 0 jumps, 0 unsupported constructs
- OnConfiguring: 4 nodes, 3 edges, 0 jumps, 0 unsupported constructs
- SemanticKnowledgeCache (DbSet property): 3 nodes, 2 edges, 0 jumps, 0 unsupported constructs
- KnowledgeUnits (DbSet property): 3 nodes, 2 edges, 0 jumps, 0 unsupported constructs
- KnowledgeUnitLinks (DbSet property): 3 nodes, 2 edges, 0 jumps, 0 unsupported constructs
- Ebooks (DbSet property): 3 nodes, 2 edges, 0 jumps, 0 unsupported constructs
- QuizResults (DbSet property): 3 nodes, 2 edges, 0 jumps, 0 unsupported constructs
- SubscriptionPlans (DbSet property): 3 nodes, 2 edges, 0 jumps, 0 unsupported constructs
- Authors (DbSet property): 3 nodes, 2 edges, 0 jumps, 0 unsupported constructs
- OnModelCreating: 23 nodes, 25 edges, 0 jumps, 0 unsupported constructs

## Diagrams

### AppDbContext Neighborhood

_Warnings: diagram file_1pacvtg_appdbcontext_cs_neighborhood_view exceeds preferred complexity thresholds (13 nodes, 0 edges)._

```mermaid
---
config:
  flowchart:
    defaultRenderer: "elk"
---
flowchart LR
    class_wtyati_appdbcontext("AppDbContext")
    file_1pacvtg_appdbcontext_cs("AppDbContext")
    method_1jwlkph_knowledgeunitlinks_dbset_property[["KnowledgeUnitLinks (DbSet property)"]]
    method_1of3kqt_onmodelcreating[["OnModelCreating()"]]
    method_1r4sskm_authors_dbset_property[["Authors (DbSet property)"]]
    method_1v6tqnu_semanticknowledgecache_dbset_property[["SemanticKnowledgeCache (DbSet property)"]]
    method_1wn8qlw_onconfiguring[["OnConfiguring()"]]
    method_cfsfui_ebooks_dbset_property[["Ebooks (DbSet property)"]]
    method_cusclc_subscriptionplans_dbset_property[["SubscriptionPlans (DbSet property)"]]
    method_ex6nhz_appdbcontext[["AppDbContext()"]]
    method_w8lt4l_quizresults_dbset_property[["QuizResults (DbSet property)"]]
    method_zj5uq4_knowledgeunits_dbset_property[["KnowledgeUnits (DbSet property)"]]
    type_c5amk9_appdbcontext[/"AppDbContext"/]
```

### AppDbContext Execution Overview

```mermaid
flowchart TD
    class_wtyati_appdbcontext("AppDbContext")
    method_1jwlkph_knowledgeunitlinks_dbset_property[["KnowledgeUnitLinks (DbSet property)"]]
    method_1of3kqt_onmodelcreating[["OnModelCreating()"]]
    method_1r4sskm_authors_dbset_property[["Authors (DbSet property)"]]
    method_1v6tqnu_semanticknowledgecache_dbset_property[["SemanticKnowledgeCache (DbSet property)"]]
    method_1wn8qlw_onconfiguring[["OnConfiguring()"]]
    method_cfsfui_ebooks_dbset_property[["Ebooks (DbSet property)"]]
    method_cusclc_subscriptionplans_dbset_property[["SubscriptionPlans (DbSet property)"]]
    method_ex6nhz_appdbcontext[["AppDbContext()"]]
    method_w8lt4l_quizresults_dbset_property[["QuizResults (DbSet property)"]]
    method_zj5uq4_knowledgeunits_dbset_property[["KnowledgeUnits (DbSet property)"]]
    class_wtyati_appdbcontext -->|contains| method_1jwlkph_knowledgeunitlinks_dbset_property
    class_wtyati_appdbcontext -->|contains| method_1of3kqt_onmodelcreating
    class_wtyati_appdbcontext -->|contains| method_1r4sskm_authors_dbset_property
    class_wtyati_appdbcontext -->|contains| method_1v6tqnu_semanticknowledgecache_dbset_property
    class_wtyati_appdbcontext -->|contains| method_1wn8qlw_onconfiguring
    class_wtyati_appdbcontext -->|contains| method_cfsfui_ebooks_dbset_property
    class_wtyati_appdbcontext -->|contains| method_cusclc_subscriptionplans_dbset_property
    class_wtyati_appdbcontext -->|contains| method_ex6nhz_appdbcontext
    class_wtyati_appdbcontext -->|contains| method_w8lt4l_quizresults_dbset_property
    class_wtyati_appdbcontext -->|contains| method_zj5uq4_knowledgeunits_dbset_property
```

### OnModelCreating() Control Flow

_Warnings: diagram method_1of3kqt_onmodelcreating__fragment_diagram exceeds preferred complexity thresholds (23 nodes, 25 edges)._

```mermaid
---
config:
  flowchart:
    defaultRenderer: "elk"
---
flowchart TD
    method_1of3kqt_onmodelcreating__action_111_entity_configuration_111__linear("QuizResult relationship and index")
    method_1of3kqt_onmodelcreating__action_121_data_seed_121__linear("Seed SubscriptionPlan rows with deterministic IDs")
    method_1of3kqt_onmodelcreating__action_30_framework_call_30__linear("Calls base OnModelCreating")
    method_1of3kqt_onmodelcreating__action_32_entity_configuration_32__linear("NexusUser property & index mapping")
    method_1of3kqt_onmodelcreating__action_48_entity_configuration_48__linear("SubscriptionPlan unique index")
    method_1of3kqt_onmodelcreating__action_53_entity_configuration_53__linear("SemanticKnowledgeCache key/index and provider-specific embedding handling")
    method_1of3kqt_onmodelcreating__action_58_branch_58__condition{"Database.IsNpgsql()"}
    method_1of3kqt_onmodelcreating__action_58_branch_58__false("other-db-path")
    method_1of3kqt_onmodelcreating__action_58_branch_58__merge("merge")
    method_1of3kqt_onmodelcreating__action_58_branch_58__true("postgresql-path")
    method_1of3kqt_onmodelcreating__action_58_onmodelcreating_branch_58_0__condition{"Database.IsNpgsql()"}
    method_1of3kqt_onmodelcreating__action_58_onmodelcreating_branch_58_0__false("false")
    method_1of3kqt_onmodelcreating__action_58_onmodelcreating_branch_58_0__merge("merge")
    method_1of3kqt_onmodelcreating__action_58_onmodelcreating_branch_58_0__true("true")
    method_1of3kqt_onmodelcreating__action_64_onmodelcreating_fallback_64_1__condition{"Falls back to alternate path"}
    method_1of3kqt_onmodelcreating__action_64_onmodelcreating_fallback_64_1__false("otherwise")
    method_1of3kqt_onmodelcreating__action_64_onmodelcreating_fallback_64_1__merge("merge")
    method_1of3kqt_onmodelcreating__action_64_onmodelcreating_fallback_64_1__true("fallback")
    method_1of3kqt_onmodelcreating__action_70_entity_configuration_70__linear("KnowledgeUnit keys, indexes, relationship to Ebook")
    method_1of3kqt_onmodelcreating__action_82_entity_configuration_82__linear("KnowledgeUnitLink relationships")
    method_1of3kqt_onmodelcreating__action_96_entity_configuration_96__linear("Ebook relationships")
    method_1of3kqt_onmodelcreating__entry(["OnModelCreating entry"])
    method_1of3kqt_onmodelcreating__implicit_return(["return"])
    method_1of3kqt_onmodelcreating__action_111_entity_configuration_111__linear -->|next| method_1of3kqt_onmodelcreating__action_121_data_seed_121__linear
    method_1of3kqt_onmodelcreating__action_121_data_seed_121__linear -->|return| method_1of3kqt_onmodelcreating__implicit_return
    method_1of3kqt_onmodelcreating__action_30_framework_call_30__linear -->|next| method_1of3kqt_onmodelcreating__action_32_entity_configuration_32__linear
    method_1of3kqt_onmodelcreating__action_32_entity_configuration_32__linear -->|next| method_1of3kqt_onmodelcreating__action_48_entity_configuration_48__linear
    method_1of3kqt_onmodelcreating__action_48_entity_configuration_48__linear -->|next| method_1of3kqt_onmodelcreating__action_53_entity_configuration_53__linear
    method_1of3kqt_onmodelcreating__action_53_entity_configuration_53__linear -->|next| method_1of3kqt_onmodelcreating__action_58_branch_58__condition
    method_1of3kqt_onmodelcreating__action_58_branch_58__condition -->|other-db-path| method_1of3kqt_onmodelcreating__action_58_branch_58__false
    method_1of3kqt_onmodelcreating__action_58_branch_58__condition -->|postgresql-path| method_1of3kqt_onmodelcreating__action_58_branch_58__true
    method_1of3kqt_onmodelcreating__action_58_branch_58__false -->|next| method_1of3kqt_onmodelcreating__action_58_branch_58__merge
    method_1of3kqt_onmodelcreating__action_58_branch_58__merge -->|next| method_1of3kqt_onmodelcreating__action_58_onmodelcreating_branch_58_0__condition
    method_1of3kqt_onmodelcreating__action_58_branch_58__true -->|next| method_1of3kqt_onmodelcreating__action_58_branch_58__merge
    method_1of3kqt_onmodelcreating__action_58_onmodelcreating_branch_58_0__condition -->|false| method_1of3kqt_onmodelcreating__action_58_onmodelcreating_branch_58_0__false
    method_1of3kqt_onmodelcreating__action_58_onmodelcreating_branch_58_0__condition -->|true| method_1of3kqt_onmodelcreating__action_58_onmodelcreating_branch_58_0__true
    method_1of3kqt_onmodelcreating__action_58_onmodelcreating_branch_58_0__false -->|next| method_1of3kqt_onmodelcreating__action_58_onmodelcreating_branch_58_0__merge
    method_1of3kqt_onmodelcreating__action_58_onmodelcreating_branch_58_0__merge -->|fallback| method_1of3kqt_onmodelcreating__action_64_onmodelcreating_fallback_64_1__condition
    method_1of3kqt_onmodelcreating__action_58_onmodelcreating_branch_58_0__true -->|next| method_1of3kqt_onmodelcreating__action_58_onmodelcreating_branch_58_0__merge
    method_1of3kqt_onmodelcreating__action_64_onmodelcreating_fallback_64_1__condition -->|otherwise| method_1of3kqt_onmodelcreating__action_64_onmodelcreating_fallback_64_1__false
    method_1of3kqt_onmodelcreating__action_64_onmodelcreating_fallback_64_1__condition -->|fallback| method_1of3kqt_onmodelcreating__action_64_onmodelcreating_fallback_64_1__true
    method_1of3kqt_onmodelcreating__action_64_onmodelcreating_fallback_64_1__false -->|next| method_1of3kqt_onmodelcreating__action_64_onmodelcreating_fallback_64_1__merge
    method_1of3kqt_onmodelcreating__action_64_onmodelcreating_fallback_64_1__merge -->|next| method_1of3kqt_onmodelcreating__action_70_entity_configuration_70__linear
    method_1of3kqt_onmodelcreating__action_64_onmodelcreating_fallback_64_1__true -->|next| method_1of3kqt_onmodelcreating__action_64_onmodelcreating_fallback_64_1__merge
    method_1of3kqt_onmodelcreating__action_70_entity_configuration_70__linear -->|next| method_1of3kqt_onmodelcreating__action_82_entity_configuration_82__linear
    method_1of3kqt_onmodelcreating__action_82_entity_configuration_82__linear -->|next| method_1of3kqt_onmodelcreating__action_96_entity_configuration_96__linear
    method_1of3kqt_onmodelcreating__action_96_entity_configuration_96__linear -->|next| method_1of3kqt_onmodelcreating__action_111_entity_configuration_111__linear
    method_1of3kqt_onmodelcreating__entry -->|next| method_1of3kqt_onmodelcreating__action_30_framework_call_30__linear
```

## Diagram Validation

- Status: warn
- Warning: neighborhood-view: diagram file_1pacvtg_appdbcontext_cs_neighborhood_view exceeds preferred complexity thresholds (13 nodes, 0 edges).
- Warning: control-flow: diagram method_1of3kqt_onmodelcreating__fragment_diagram exceeds preferred complexity thresholds (23 nodes, 25 edges).
