---
name: step-00-init
description: Initialize EF Core context - detect project, DbContext, and environment
next_step: null
---

# Step 0: Initialize EF Core Context

## YOUR TASK:

Detect EF Core project structure, identify DbContext (Core vs Extensions), and load environment configuration.

**This step runs first for ALL EF Core commands.**

---

## EXECUTION SEQUENCE:

Load all helper functions from: `references/shared-init-functions.md`

This includes:
- `ensure_dotnet_ef()` - Platform-aware dotnet-ef PATH fix
- `detect_efcore_project()` - Find EF Core project
- `detect_dbcontext()` - Detect Core vs Extensions
- `detect_environment()` - Load environment configuration
- `determine_base_branch()` - Determine GitFlow branch
- `block_production()` - Prevent production deployments
- `run_for_contexts()` - Helper for dual context execution

### User Prompt (if DbContext Unknown)

**If `{dbcontext}` is empty after detection:**

```yaml
AskUserQuestion:
  header: "DbContext"
  question: "Which DbContext to use?"
  options:
    - label: "CoreDbContext"
      description: "SmartStack entities (User, Role, Navigation...)"
    - label: "ExtensionsDbContext"
      description: "Client-specific entities"
    - label: "Both"
      description: "Run for both contexts"
  multiSelect: false
```

**Set variables based on response:**
```javascript
if (answer === "CoreDbContext") {
  DBCONTEXT = "CoreDbContext";
  DBCONTEXT_TYPE = "core";
  SCHEMA = "core";
} else if (answer === "ExtensionsDbContext") {
  DBCONTEXT = "ExtensionsDbContext";
  DBCONTEXT_TYPE = "extensions";
  SCHEMA = "extensions";
} else {
  // Both - will run commands twice
  RUN_BOTH = true;
}
```

### Display Summary

```
EF CORE CONTEXT
├── Project:    {PROJECT_NAME}
├── DbContext:  {DBCONTEXT} ({DBCONTEXT_TYPE})
├── Run Both:   {RUN_BOTH} (Core → Extensions)
├── Schema:     {SCHEMA}
├── Migrations: {MIGRATIONS_DIR}
├── Environment:{SELECTED_ENV}
└── Branch:     {CURRENT_BRANCH} → {BASE_BRANCH}
```

---

## STATE OUTPUT:

After this step, these variables are available:

| Variable | Example Value |
|----------|---------------|
| `{project_name}` | SmartStack.Infrastructure |
| `{dbcontext}` | CoreDbContext |
| `{dbcontext_type}` | core |
| `{schema}` | core |
| `{migrations_dir}` | src/SmartStack.Infrastructure/Persistence/Migrations |
| `{infra_project}` | src/SmartStack.Infrastructure/SmartStack.Infrastructure.csproj |
| `{startup_project}` | src/SmartStack.Api/SmartStack.Api.csproj |
| `{base_branch}` | develop |
| `{branch_type}` | feature |
| `{current_branch}` | feature/multitenant |
| `{selected_env}` | Local |
| `{run_both}` | true |

---

## SUCCESS CRITERIA:

- EF Core project detected
- DbContext identified (or user prompted)
- Environment loaded
- No production environment
- All paths validated

## FAILURE MODES:

| Error | Resolution |
|-------|------------|
| No EF Core project | Check Microsoft.EntityFrameworkCore reference |
| No appsettings.Local.json | Create file or use --env flag |
| Production detected | Use different environment |
| Both DbContexts found | User will be prompted |

---

## NEXT STEP:

Route to command-specific step based on `{command}`:

| Command | Next Step |
|---------|-----------|
| `db-status` | `../db/step-status.md` |
| `db-deploy` | `../db/step-deploy.md` |
| `db-reset` | `../db/step-reset.md` |
| `db-seed` | `../db/step-seed.md` |
| `migration` | `../migration/step-01-check.md` |
| `squash` | `../squash/step-01-backup.md` |
| `rebase-snapshot` | `../rebase-snapshot/step-01-backup.md` |
