---
description: Create or recreate the EF Core migration for the current branch (1 migration per feature)
agent: efcore-migration
model: sonnet
---

# EF Core Migration

> **Ref:** [_shared.md](_shared.md)
> **Règle:** 1 migration par feature. Si existe déjà, supprimer et recréer.

---

## STEP 0: Validation Cross-Branch

```bash
if [ "$CROSS_BRANCH_ENABLED" = "true" ]; then
  # Comparer ModelSnapshot avec develop
  DIFF_LINES=$(diff "$LOCAL_SNAPSHOT" "$DEVELOP_SNAPSHOT" 2>/dev/null | wc -l)
  if [ "$DIFF_LINES" -gt 100 ]; then
    echo "⚠️ CONFLIT CROSS-BRANCH DÉTECTÉ ($DIFF_LINES lignes)"
    echo "→ /efcore:rebase-snapshot (recommandé)"
    [ "$BLOCK_ON_CONFLICT" = "true" ] && exit 1
  fi
fi
```

Options: `--force`, `--no-cross-check`, `--rebase-first`

---

## STEP 1: Contexte Git

```bash
CURRENT_BRANCH=$(git branch --show-current)
# Extraire type (feature/hotfix/release) et nom
detect_efcore_project
```

---

## STEP 2: Recherche Migration Existante

```bash
SEARCH_PATTERN="${BRANCH_TYPE}_.*_${BRANCH_NAME}"
EXISTING_MIGRATIONS=$(find "$MIGRATIONS_DIR" -name "*.cs" | grep -E "$SEARCH_PATTERN")
```

---

## STEP 3: Décision

```javascript
// Si migration existe
AskUserQuestion({
  questions: [{
    question: "Migration existante. Que faire?",
    header: "Migration",
    options: [
      { label: "Recréer", description: "Supprimer et recréer (Recommandé)" },
      { label: "Garder", description: "Conserver, ajouter nouvelle (Non recommandé)" },
      { label: "Annuler", description: "Ne rien faire" }
    ],
    multiSelect: false
  }]
})
```

---

## STEP 4: Nom Migration

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

```javascript
AskUserQuestion({
  questions: [{
    question: "Description courte (ex: AddUserRoles)",
    header: "Description",
    options: [
      { label: "Add", description: "Ajout tables/colonnes" },
      { label: "Update", description: "Modification structure" },
      { label: "Fix", description: "Correction schéma" },
      { label: "Remove", description: "Suppression éléments" }
    ],
    multiSelect: false
  }]
})
```

---

## STEP 5: Création

```bash
dotnet ef migrations add "$MIGRATION_NAME" --verbose
```

---

## STEP 6: Validation

```javascript
AskUserQuestion({
  questions: [{
    question: "Migration correcte?",
    header: "Validation",
    options: [
      { label: "Oui, appliquer", description: "Deploy sur DB locale" },
      { label: "Oui, pas maintenant", description: "Garder sans appliquer" },
      { label: "Non, supprimer", description: "Annuler et recommencer" }
    ],
    multiSelect: false
  }]
})
```

---

## Résumé

```
MIGRATION - {MIGRATION_NAME}
├── Branche: {CURRENT_BRANCH}
├── Fichiers: {timestamp}_{NAME}.cs, Designer.cs, Snapshot.cs
├── Validations: ✓ 1 migration/feature, ✓ Convention nommage
└── Prochains: /efcore:db-deploy, /gitflow:3-commit
```

---

## Options

| Option | Description |
|--------|-------------|
| `--name {name}` | Forcer un nom spécifique |
| `--force` | Supprimer sans confirmation |
| `--no-cross-check` | Désactiver validation cross-branch |
