---
name: efcore
description: EF Core Commands - Migration and database management
argument-hint: "<command> [options]"
disable-model-invocation: true
---

## Current state (auto-injected)
- Branch: !`git branch --show-current`
- Recent migrations: !`find . -path "*/Migrations/*.cs" -not -name "*Designer*" -not -name "*Snapshot*" 2>/dev/null | sort | tail -5 || echo "none found"`

<objective>
Manage EF Core migrations and database operations with progressive step loading, MCP validation, and DbContext auto-detection.
</objective>

<quick_start>
**Database operations:**
```bash
/efcore db-status          # Check migration status
/efcore db-deploy          # Apply pending migrations
/efcore db-reset           # Drop + recreate database
/efcore db-seed            # Populate with test data
```

**Migration operations:**
```bash
/efcore migration          # Create migration for current branch
/efcore squash             # Consolidate branch migrations
/efcore rebase-snapshot    # Sync snapshot with parent branch
/efcore scan               # Scan for cross-branch conflicts
/efcore conflicts          # Analyze migration conflicts
```
</quick_start>

<parameters>

<subcommands>
| Command | Description | Steps |
|---------|-------------|-------|
| `db-status` | Display migrations and database status | steps/db/status/ |
| `db-deploy` | Apply pending migrations | steps/db/deploy/ |
| `db-reset` | Drop + recreate database | steps/db/reset/ |
| `db-seed` | Populate with test data | steps/db/seed/ |
| `migration` | Create/recreate migration | steps/migration/ |
| `squash` | Consolidate migrations | steps/squash/ |
| `rebase-snapshot` | Sync snapshot with parent | steps/rebase-snapshot/ |
| `scan` | Scan for conflicts | (MCP-centric) |
| `conflicts` | Analyze conflicts | (MCP-centric) |
</subcommands>

<flags>
| Flag | Description |
|------|-------------|
| `--context <name>` | Specify DbContext (CoreDbContext/ExtensionsDbContext) |
| `--env <name>` | Use appsettings.{name}.json |
| `--verbose` | Show detailed output |
| `--force` | Skip confirmations |
| `--dry-run` | Preview without executing |
</flags>

</parameters>

<state_variables>
**Shared state across all steps:**

| Variable | Type | Description |
|----------|------|-------------|
| `{command}` | string | Current subcommand |
| `{dbcontext}` | string | CoreDbContext or ExtensionsDbContext |
| `{dbcontext_type}` | string | "core" or "extensions" |
| `{schema}` | string | Database schema |
| `{infra_project}` | string | Path to Infrastructure project |
| `{startup_project}` | string | Path to API/Web project |
| `{migrations_dir}` | string | Path to Migrations folder |
| `{base_branch}` | string | Parent branch (develop/main) |
| `{branch_type}` | string | feature/release/hotfix |
| `{auto_mode}` | boolean | Skip confirmations |
</state_variables>

<entry_point>

**ROUTING LOGIC:**

1. Parse command and flags from input
2. Load shared init step: `steps/shared/step-00-init.md`
3. Route to command-specific steps:

| Command | Route To |
|---------|----------|
| `db-status` | `steps/db/step-status.md` |
| `db-deploy` | `steps/db/step-deploy.md` |
| `db-reset` | `steps/db/step-reset.md` |
| `db-seed` | `steps/db/step-seed.md` |
| `migration` | `steps/migration/step-00-init.md` |
| `squash` | `steps/squash/step-00-init.md` |
| `rebase-snapshot` | `steps/rebase-snapshot/step-00-init.md` |
| `scan` | Use `commands/efcore/scan.md` (MCP-centric, no steps) |
| `conflicts` | Use `commands/efcore/conflicts.md` (MCP-centric, no steps) |

</entry_point>

<shared_functions>
**Always load first:** `steps/shared/step-00-init.md`

This step provides:
- `detect_efcore_project()` - Find EF Core project files
- `detect_dbcontext()` - Auto-detect Core vs Extensions
- `detect_environment()` - Load connection string
- `determine_base_branch()` - Find parent branch
- `block_production()` - Safety check

**MCP Integration:**
- `mcp__smartstack__suggest_migration` - Generate compliant names
- `mcp__smartstack__check_migrations` - Analyze conflicts
- `mcp__smartstack__validate_conventions` - Validate structure
</shared_functions>

<dual_dbcontext>
SmartStack uses **two separate DbContexts**:

| Context | DbContext | Schema | History Table |
|---------|-----------|--------|---------------|
| `core` | `CoreDbContext` | `core` | `core.__EFMigrationsHistory` |
| `extensions` | `ExtensionsDbContext` | `extensions` | `extensions.__EFMigrationsHistory` |

**Auto-detection priority:**
1. SmartStack.Domain exists → CoreDbContext
2. Client with SmartStack NuGet → ExtensionsDbContext
3. Code scan for DbContext classes
4. Ask user if ambiguous
</dual_dbcontext>

<naming_convention>
**Migration naming (MCP MANDATORY):**

Pattern: `{context}_v{version}_{sequence}_{Description}`

Examples:
- `core_v1.9.0_001_AddUserRoles`
- `extensions_v1.2.0_001_AddCustomFields`

**FORBIDDEN:** Manual name calculation. Always use MCP.
</naming_convention>

<safety_rules>
1. **Backup MANDATORY** for destructive operations
2. **Production BLOCKED** - Never run on production
3. **1 migration per feature** - Recreate, don't accumulate
4. **Snapshot + Migrations** - Always recover BOTH from parent
5. **MCP for naming** - Never hardcode migration names
</safety_rules>

<execution_rules>
- **Load one step at a time** - Progressive loading
- **State persists** - Variables carry across steps
- **Follow next_step** - Each step defines next
- **MCP validation** - Use SmartStack tools
- **Safety > Correctness > Speed**
</execution_rules>

<success_criteria>
- DbContext correctly detected
- Migrations created with MCP naming
- Database operations succeed
- Parent migrations preserved (for squash/rebase)
- Build passes after changes
</success_criteria>
