---
description: Display migrations and database status
agent: efcore-db-status
model: haiku
---

# EF Core Database Status

Displays the complete status of EF Core migrations and database connection using **SmartStack MCP**.

**Quick command, read-only, no modifications.**

**INTEGRATION:** Uses `mcp__smartstack__check_migrations` for migration analysis.

---

## STEP 1: Detect Configuration

```bash
# Check config files
CONFIG_LOCAL="appsettings.Local.json"
CONFIG_DEV="appsettings.Development.json"
CONFIG_DEFAULT="appsettings.json"

if [ -f "$CONFIG_LOCAL" ]; then
  CONFIG_USED="$CONFIG_LOCAL"
elif [ -f "$CONFIG_DEV" ]; then
  CONFIG_USED="$CONFIG_DEV"
else
  CONFIG_USED="$CONFIG_DEFAULT"
fi

# Detect EF Core project
CSPROJ=$(find . -name "*.csproj" -exec grep -l "Microsoft.EntityFrameworkCore" {} \; | head -1)
PROJECT_DIR=$(dirname "$CSPROJ")
PROJECT_NAME=$(basename "$CSPROJ" .csproj)
```

---

## STEP 2: Invoke MCP for Migration Status

> **MCP INTEGRATION:** Get structured migration info from SmartStack MCP.

```json
{
  "tool": "mcp__smartstack__check_migrations",
  "parameters": {
    "branch": "<current_branch>"
  }
}
```

**Claude instruction:** Call the MCP tool to get migration list:

```
CALL mcp__smartstack__check_migrations WITH:
  - branch: {current_branch}

EXTRACT from response:
  - migrations[] -> list of all migrations
  - conflicts[] -> any naming/ordering issues
```

---

## STEP 3: Test Database Connection

```bash
cd "$PROJECT_DIR"

# Test connection with EF Core
CONNECTION_OK=$(dotnet ef database list 2>&1)
if echo "$CONNECTION_OK" | grep -q "error\|Error\|failed"; then
  CONNECTION_STATUS="FAILED"
  CONNECTION_ERROR="$CONNECTION_OK"
else
  CONNECTION_STATUS="OK"
fi
```

---

## STEP 4: Get Applied vs Pending

```bash
# Migrations in code vs applied
CODE_MIGRATIONS=$(dotnet ef migrations list 2>/dev/null | grep -v "^Build" | grep -v "^$")
APPLIED=$(dotnet ef migrations list 2>/dev/null | grep -v "(Pending)" | grep -v "^Build" | wc -l)
PENDING=$(dotnet ef migrations list 2>/dev/null | grep "(Pending)" | wc -l)
```

---

## STEP 5: Display Report

```
================================================================================
                         EF CORE - DATABASE STATUS
================================================================================

PROJECT
  Name:          {PROJECT_NAME}
  Config:        {CONFIG_USED}
  DbContext:     ApplicationDbContext

CONNECTION
  Status:        {CONNECTION_STATUS}
  Server:        {SERVER}
  Database:      {DATABASE}

MIGRATIONS (from MCP)
  Total:         {migrations.length}
  Applied:       {APPLIED}
  Pending:       {PENDING} {if > 0: "-> /efcore:db-deploy"}

NAMING CONVENTION CHECK (from MCP)
  Format:        {context}_v{version}_{sequence}_{Description}
  Compliance:    {conflicts.filter(c => c.type === 'naming').length === 0 ? "OK" : "WARNINGS"}

================================================================================
```

---

## STEP 5.1: Display Migration List (from MCP)

```
MIGRATIONS LIST
--------------------------------------------------------------------------------
  #  | Name                              | Context | Version | Sequence
--------------------------------------------------------------------------------
{for each migration in migrations}
  {i} | {migration.name}                 | {migration.context} | {migration.version} | {migration.sequence}
{end for}
--------------------------------------------------------------------------------
```

---

## STEP 5.2: Check "1 Migration per Feature" Rule

```bash
CURRENT_BRANCH=$(git branch --show-current)

# Extract branch type
if [[ "$CURRENT_BRANCH" == feature/* ]]; then
  BRANCH_TYPE="Feature"
elif [[ "$CURRENT_BRANCH" == hotfix/* ]]; then
  BRANCH_TYPE="Hotfix"
fi
```

**Display:**

```
"1 MIGRATION PER FEATURE" RULE
--------------------------------------------------------------------------------
  Branch:       {CURRENT_BRANCH}
  Migrations:   {count for this branch}
  Status:       {OK if count <= 1, else WARNING}
--------------------------------------------------------------------------------
```

---

## STEP 6: Show Warnings from MCP

If MCP returned conflicts or warnings:

```
WARNINGS (from MCP analysis)
--------------------------------------------------------------------------------
{for each conflict in conflicts}
  [{conflict.type}] {conflict.description}
    Resolution: {conflict.resolution}
{end for}
--------------------------------------------------------------------------------
```

---

## STEP 7: Connection Error Handling

**If connection error:**

```
CONNECTION PROBLEM
--------------------------------------------------------------------------------
{CONNECTION_ERROR}
--------------------------------------------------------------------------------

TROUBLESHOOTING:
  1. Is SQL Server running?
  2. Is appsettings.Local.json configured?
  3. Does the database exist?

COMMANDS:
  /efcore:db-reset   -> Create/recreate database
  /gitflow:10-start  -> Configure appsettings.Local.json
```

---

## Options

| Option | Description |
|--------|-------------|
| `--verbose` | Display all migrations with details |
| `--json` | JSON output for scripting |
| `--context {name}` | Specify the DbContext |

---

## MCP Tool Reference

**Tool:** `mcp__smartstack__check_migrations`

**Used for:**
- Getting structured list of migrations (name, context, version, sequence)
- Detecting naming convention violations
- Checking migration order

**Response fields used:**
- `migrations[]` - List of all migrations with parsed metadata
- `conflicts[]` - Any issues detected (naming, order)
