entity-framework について
EF Core を扱う開発者向けのスキルです。Sorcha の EF Core 9 以降と PostgreSQL によるデータアクセスのパターンを、リポジトリ、論理削除、マイグレーションを含めて説明します。
主な機能
- 01PostgreSQL の DbContext 設定
- 02汎用・専用リポジトリのパターン
- 03論理削除のクエリフィルター
- 04マイグレーションの作成と適用
- 05JSONB と一括更新
活用シーン
- 01Sorcha サービスのデータベース設定
- 02関連データを任意で読み込むリポジトリの実装
- 03論理削除データの管理
- 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) |
| JSONB | PostgreSQL の 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);
関連項目
関連スキル
- 接続の構成や 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"
コメント
Markdown 対応