ef-core

A guide to Entity Framework Core, a .NET tool for working with data in databases through C# objects and queries.

In plain words
What is it for?
Use it when defining entities, configuring database tables, writing repositories, querying data, or changing the database schema.
Why use it?
It sets consistent patterns for database contexts, table mappings, repositories, migrations, and query performance.

Cursor rule for Cursor

Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

agentmods
npx agentmods add rules/goranerhartic/cursor-development-rules/ef-core
Clone the repo
git clone --depth 1 https://github.com/GoranErhartic/cursor-development-rules

Made for: Cursor.

Per session 14 Only the description is in the session, so the agent can decide to use it. The body loads when it is invoked.
When invoked 922 The whole file, excluding the scripts and references it only reads on demand.
Security scan A 0 findings. Scan, not verified.
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5 $0.00014 $0.00922
Opus 5 $0.00007 $0.00461
Sonnet 5 $0.00003 $0.00184
Haiku 4.5 $0.00001 $0.00092

Measured 3d ago against content hash 1b38d4369c86, method: parsed. Prices are Anthropic first-party input rates as of 2026-08-30, from the pricing page.

Security

Grade A, and why

ef-core scanned grade A with 0 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured 3d ago.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Nothing flagged

None of the 26 patterns this scan looks for appear in this file: no shell pipes, no recursive deletes, no credential paths, no hidden text, no instruction-override or anti-refusal phrasing, no agent-config snooping. That is not a guarantee, it is the absence of the things that are checkable.

.cursor/rules/languages/csharp/ef-core.mdc · 154 lines

How it starts

The opening of the file, as written. The whole thing — 154 lines — stays where its author put it; the contents beside it link to each section on GitHub.

Entity Framework Core Patterns

DbContext Configuration

public sealed class AppDbContext : DbContext
{
    public AppDbContext(DbContextOptions<AppDbContext> options) : base(options) { }

    public DbSet<User> Users => Set<User>();

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.ApplyConfigurationsFromAssembly(typeof(AppDbContext).Assembly);
    }
}

Entity Configuration

public sealed class UserConfiguration : IEntityTypeConfiguration<User>
{
    public void Configure(EntityTypeBuilder<User> builder)
    {
        builder.ToTable("Users");
        builder.HasKey(x => x.Id);
        builder.Property(x => x.Email).HasMaxLength(256).IsRequired();
        builder.HasIndex(x => x.Email).IsUnique();
    }
}

Repository Pattern

public interface IUserRepository
{
    Task<User?> GetByIdAsync(Guid id, CancellationToken ct = default);
    Task<User?> GetByEmailAsync(string email, CancellationToken ct = default);
    void Add(User user);
    void Remove(User user);
}

Query Best Practices

Scenario Approach
Read-only queries .AsNoTracking()
Single item .FirstOrDefaultAsync() (not .SingleOrDefaultAsync() unless uniqueness matters)
Exists check .AnyAsync()
Count .CountAsync()
Projections .Select(x => new Dto { ... }) — avoid loading full entities
Pagination .Skip(n).Take(m) after ordering

Anti-Patterns to Avoid

  • NEVER use .ToList() then LINQ — query in database
  • NEVER load navigation properties you don't need
  • NEVER use Include() for read-only projections — use Select()
  • NEVER call SaveChangesAsync() multiple times per operation

Migrations

dotnet ef migrations add MigrationName --project src/Infrastructure --startup-project src/Api
dotnet ef database update --project src/Infrastructure --startup-project src/Api
  • ALWAYS review generated migrations before applying
  • NEVER modify existing migrations after they're applied

Read the full file on GitHub · 154 lines

Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. 3d ago First seen · 154 lines · 14 tokens per session scan A 1b38d4369c86

Subscribe to this mod's changes

ef-core is a cursor rule published in the GitHub repository GoranErhartic/cursor-development-rules (19 stars, last pushed 6mo ago), licensed MIT. It adds 14 tokens to every session and 922 once invoked, about $0.0001 per session on Opus 5. A static security scan graded it A with 0 findings. No closer match exists in the catalogue, so it is treated as the original; first seen 2026-08-30.