---
description: Deploy EF Core migrations to local database
agent: efcore-db-deploy
model: haiku
---

# EF Core Database Deploy

Applies all pending migrations to the configured database.

---

## STEP 0: Verification environnement et connexion

> **Include:** `_env-check.md` (etapes 0.1 a 0.4)

### 0.1 Detecter les fichiers appsettings

```bash
# Scanner les fichiers appsettings
API_DIR=$(find . -type d -name "*.Api" | head -1)
[ -z "$API_DIR" ] && API_DIR="src/SmartStack.Api"

APPSETTINGS_FILES=$(find "$API_DIR" -maxdepth 1 -name "appsettings*.json" 2>/dev/null | sort)
```

### 0.2 Selectionner l'environnement

```bash
# Priorite: --env flag > appsettings.Local.json > erreur
ENV_FLAG="${1#--env=}"  # Extraire valeur si --env=Development

if [ -n "$ENV_FLAG" ] && [ "$ENV_FLAG" != "$1" ]; then
  SELECTED_ENV="appsettings.${ENV_FLAG}.json"
elif [ -f "$API_DIR/appsettings.Local.json" ]; then
  SELECTED_ENV="appsettings.Local.json"
else
  echo "ERROR: Aucun fichier appsettings trouve"
  exit 1
fi
```

### 0.3 Extraire et afficher la connexion

```bash
# Extraire connection string
CONNECTION_STRING=$(powershell -Command "
  \$json = Get-Content '$API_DIR/$SELECTED_ENV' | ConvertFrom-Json
  \$json.ConnectionStrings.DefaultConnection
")

DATABASE_NAME=$(echo "$CONNECTION_STRING" | grep -oP 'Database=\K[^;]+')
SERVER_NAME=$(echo "$CONNECTION_STRING" | grep -oP 'Server=\K[^;]+')
CONNECTION_MASKED=$(echo "$CONNECTION_STRING" | sed 's/Password=[^;]*/Password=****/g')
```

### 0.4 Afficher le resume

```
================================================================================
                         EF CORE - DATABASE DEPLOY
================================================================================

FICHIERS DISPONIBLES:
{liste des appsettings detectes}

ENVIRONNEMENT: {SELECTED_ENV}

CONNEXION CIBLE:
  Server:   {SERVER_NAME}
  Database: {DATABASE_NAME}
  String:   {CONNECTION_MASKED}

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

### 0.5 Protection Production

```bash
# Bloquer si Production
if [[ "$SELECTED_ENV" == *"Production"* ]]; then
  echo "BLOQUE: Environnement Production detecte"
  echo "  Utilisez un environnement non-production"
  exit 1
fi
```

---

## STEP 1: Detecter le projet EF Core

```bash
# Detect EF Core project
CSPROJ=$(find . -name "*.csproj" -exec grep -l "Microsoft.EntityFrameworkCore" {} \; | head -1)
if [ -z "$CSPROJ" ]; then
  echo "ERROR: No EF Core project detected"
  exit 1
fi

PROJECT_DIR=$(dirname "$CSPROJ")
PROJECT_NAME=$(basename "$CSPROJ" .csproj)
```

---

## STEP 2: Check pending migrations

```bash
cd "$PROJECT_DIR"
dotnet ef migrations list --json 2>/dev/null | grep -E '"applied": false'
PENDING_COUNT=$(dotnet ef migrations list --json 2>/dev/null | grep -c '"applied": false' || echo "0")
```

**Display status:**

```
================================================================================
                         EF CORE - DATABASE DEPLOY
================================================================================

PROJECT:     {PROJECT_NAME}
CONFIG:      appsettings.Local.json
MIGRATIONS:  {PENDING_COUNT} pending

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

---

## STEP 3: Apply migrations

```bash
# Apply migrations with local config
dotnet ef database update --configuration Release --verbose
```

**If connection error:**

```
⚠️  Database connection error

CHECKS:
1. Is SQL Server running?
2. Does the database exist?
3. Are the credentials correct?

USEFUL COMMANDS:
- Check connection:  sqlcmd -S {SERVER} -E
- Create database:   /efcore:db-reset
```

---

## STEP 4: Confirmation

```
================================================================================
                         DEPLOYMENT COMPLETE
================================================================================

✓ {N} migration(s) applied
✓ Database is up to date

NEXT COMMANDS:
  /efcore:db-status  → Check status
  /efcore:db-seed    → Populate test data

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

---

## Options

| Option | Description |
|--------|-------------|
| `--env {name}` | Utiliser appsettings.{name}.json (ex: --env Development) |
| `--verbose` | Display SQL details |
| `--connection "..."` | Override connection string |
| `--context {name}` | Specify DbContext if multiple |

## Exemples

```bash
# Deployer sur la base locale (defaut)
/efcore:db-deploy

# Deployer sur l'environnement Development
/efcore:db-deploy --env Development

# Deployer sur Staging
/efcore:db-deploy --env Staging
```
