mcpField
E
entity-framework

entity-framework

github.com/stuartf303データ・データベース42 回インストール
リポジトリ

entity-framework について

EF Core を扱う開発者向けのスキルです。Sorcha の EF Core 9 以降と PostgreSQL によるデータアクセスのパターンを、リポジトリ、論理削除、マイグレーションを含めて説明します。

主な機能

  1. 01PostgreSQL の DbContext 設定
  2. 02汎用・専用リポジトリのパターン
  3. 03論理削除のクエリフィルター
  4. 04マイグレーションの作成と適用
  5. 05JSONB と一括更新

活用シーン

  1. 01Sorcha サービスのデータベース設定
  2. 02関連データを任意で読み込むリポジトリの実装
  3. 03論理削除データの管理
  4. 04データベースマイグレーションの作成と適用

インストール

コマンドをコピーしてターミナルで実行します。

$npx skills add https://github.com/stuartf303/sorcha --skill entity-framework

インストール後、Claude Code で/entity-frameworkを入力して読み込まれたか確認します。

同梱ファイルreferences/patterns.mdreferences/workflows.md

詳細

Entity Framework Core スキル

Sorcha では、主要なリレーショナルデータストアとして PostgreSQL(Npgsql)と EF Core 9+ を使用します。コードベースでは、汎用リポジトリと特化型リポジトリを備えたレイヤー化されたリポジトリパターン、クエリフィルターによる論理削除、起動時の自動マイグレーションを実装しています。

クイックスタート

PostgreSQL を使用して DbContext を登録する
// src/Common/Sorcha.Storage.EFCore/Extensions/EFCoreServiceExtensions.cs
services.AddDbContext<WalletDbContext>((sp, options) =>
{
    var dataSource = sp.GetRequiredService<NpgsqlDataSource>();
    options.UseNpgsql(dataSource, npgsql =>
    {
        npgsql.EnableRetryOnFailure(maxRetryCount: 10, maxRetryDelay: TimeSpan.FromSeconds(30), errorCodesToAdd: null);
        npgsql.MigrationsHistoryTable("__EFMigrationsHistory", "wallet");
    });
});
マイグレーションを作成する
# From project directory containing DbContext
dotnet ef migrations add InitialSchema --project src/Common/Sorcha.Wallet.Core --startup-project src/Services/Sorcha.Wallet.Service
プログラムからマイグレーションを適用する
// Startup pattern used in Sorcha services
var pending = await dbContext.Database.GetPendingMigrationsAsync();
if (pending.Any())
    await dbContext.Database.MigrateAsync();

主な概念

概念用途例
DbContextスキーマ定義+変更追跡WalletDbContext、TenantDbContext
リポジトリデータアクセスの抽象化EFCoreRepository<T, TId, TContext>
論理削除DeletedAt に対するクエリフィルター.HasQueryFilter(e => e.DeletedAt == null)
JSONBPostgreSQL の JSON 列.HasColumnType("jsonb")
ExecuteUpdateデータを読み込まずに一括更新ExecuteUpdateAsync(s => s.SetProperty(...))

一般的なパターン

オプションの関連データの即時読み込みを備えたリポジトリ
// src/Common/Sorcha.Wallet.Core/Repositories/EfCoreWalletRepository.cs
public async Task<WalletEntity?> GetByAddressAsync(string address, bool includeAddresses = false)
{
    IQueryable<WalletEntity> query = _context.Wallets;
    
    if (includeAddresses)
        query = query.Include(w => w.Addresses);
    
    return await query.AsNoTracking().FirstOrDefaultAsync(w => w.Address == address);
}
フィルターを回避する論理削除
// Bypass query filter for admin operations
var deleted = await _context.Wallets
    .IgnoreQueryFilters()
    .FirstOrDefaultAsync(w => w.Address == address);

関連項目

  • パターン - DbContext の構成、エンティティのマッピング、クエリの最適化
  • ワークフロー - マイグレーションコマンド、テストパターン、デプロイ

関連スキル

  • 接続の構成や PostgreSQL 固有の機能については、postgresql スキルを参照してください
  • サービス登録やヘルスチェックについては、aspire スキルを参照してください
  • InMemory プロバイダーを使用した DbContext のテストについては、xunit スキルを参照してください

ドキュメントリソース

Context7 で EF Core の最新ドキュメントを取得します。

ライブラリ ID: /dotnet/entityframework.docs

推奨クエリ:

  • "DbContext pooling configuration dependency injection"
  • "migrations code-first apply production deployment"
  • "query filters soft delete global filters"
  • "bulk operations ExecuteUpdate ExecuteDelete"

コメント