一键导入
database
Efficient database operations with PostgreSQL, Entity Framework Core, migrations, indexing strategies, transactions, and connection pooling.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
Efficient database operations with PostgreSQL, Entity Framework Core, migrations, indexing strategies, transactions, and connection pooling.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
基于 SOC 职业分类
Build production-ready AI agents with Microsoft Foundry and Agent Framework. Covers agent architecture, model selection, orchestration, tracing, and evaluation.
Design robust REST APIs with proper versioning, pagination, error handling, rate limiting, and OpenAPI documentation.
Structure projects for maintainability and scalability with clean architecture, separation of concerns, and consistent project layouts.
Systematic code review and audit practices including automated checks, security audits, compliance verification, and review checklists.
Manage application configuration with environment variables, Azure Key Vault, feature flags, and environment-specific settings.
Fundamental coding principles for production development including SOLID, DRY, KISS, and common design patterns with C# examples.
| name | database |
| description | Efficient database operations with PostgreSQL, Entity Framework Core, migrations, indexing strategies, transactions, and connection pooling. |
Purpose: Efficient, reliable database operations with migrations, indexes, and transactions.
Stack: PostgreSQL + Entity Framework Core + Npgsql.
builder.Services.AddDbContext<AppDbContext>(options =>
options.UseNpgsql(
builder.Configuration.GetConnectionString("Default"),
npgsqlOptions =>
{
npgsqlOptions.EnableRetryOnFailure(maxRetryCount: 3);
npgsqlOptions.CommandTimeout(30);
}));
# Create migration
dotnet ef migrations add InitialCreate
# Update database
dotnet ef database update
# Generate SQL script
dotnet ef migrations script --output migration.sql
# Rollback
dotnet ef database update PreviousMigration
public partial class CreateUsersTable : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Users",
columns: table => new
{
Id = table.Column<int>(nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy",
NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
Email = table.Column<string>(maxLength: 255, nullable: false),
Name = table.Column<string>(maxLength: 100, nullable: false),
CreatedAt = table.Column<DateTime>(nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Users", x => x.Id);
});
migrationBuilder.CreateIndex(
name: "IX_Users_Email",
table: "Users",
column: "Email",
unique: true);
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(name: "Users");
}
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
// Single column index
modelBuilder.Entity<User>()
.HasIndex(u => u.Email)
.IsUnique();
// Composite index
modelBuilder.Entity<Post>()
.HasIndex(p => new { p.UserId, p.CreatedAt });
// Filtered index
modelBuilder.Entity<Order>()
.HasIndex(o => o.Status)
.HasFilter("Status = 'Active'");
}
When to Add Indexes:
using var transaction = await _context.Database.BeginTransactionAsync();
try
{
var user = new User { Email = "test@example.com" };
_context.Users.Add(user);
await _context.SaveChangesAsync();
var profile = new Profile { UserId = user.Id };
_context.Profiles.Add(profile);
await _context.SaveChangesAsync();
await transaction.CommitAsync();
}
catch
{
await transaction.RollbackAsync();
throw;
}
// Connection string with pooling
"Server=localhost;Database=myapp;User Id=user;Password=pass;Pooling=true;MinPoolSize=0;MaxPoolSize=100;"
// ❌ Loads entire entity
var users = await _context.Users.ToListAsync();
// ✅ Load only needed fields
var users = await _context.Users
.Select(u => new { u.Id, u.Name, u.Email })
.ToListAsync();
// ❌ N+1 queries
var users = await _context.Users.ToListAsync();
foreach (var user in users)
{
var posts = await _context.Posts.Where(p => p.UserId == user.Id).ToListAsync();
}
// ✅ Eager loading
var users = await _context.Users
.Include(u => u.Posts)
.ToListAsync();
// For read-only queries
var users = await _context.Users
.AsNoTracking()
.ToListAsync();
public interface IRepository<T> where T : class
{
Task<T?> GetByIdAsync(int id);
Task<IEnumerable<T>> GetAllAsync();
Task<T> AddAsync(T entity);
Task UpdateAsync(T entity);
Task DeleteAsync(int id);
}
public class Repository<T> : IRepository<T> where T : class
{
protected readonly AppDbContext _context;
protected readonly DbSet<T> _dbSet;
public Repository(AppDbContext context)
{
_context = context;
_dbSet = context.Set<T>();
}
public async Task<T?> GetByIdAsync(int id) =>
await _dbSet.FindAsync(id);
public async Task<IEnumerable<T>> GetAllAsync() =>
await _dbSet.ToListAsync();
public async Task<T> AddAsync(T entity)
{
await _dbSet.AddAsync(entity);
await _context.SaveChangesAsync();
return entity;
}
}
See Also: 05-performance.md • 07-scalability.md
Last Updated: January 13, 2026