Cursor rules for .NET engineers
Generic Cursor prompts treat every C# repo like a tutorial sample. Senior teams need AI coding rules that load only where they apply, plus architectural persistence for Cursor so decisions survive new chats. This guide covers how Cursor's rules actually load, a rule layout for a layered .NET solution, migrating an old .cursorrules file, and when AGENTS.md is enough. The Agentic Architect kit is one implementation; everything below works with rules you write yourself.
How Cursor rules work, in one minute
Language models don't retain memory between sessions. Rules are Cursor's answer: when a rule applies, its contents are included at the start of the model's context, so the agent gets the same guidance every time without you retyping it. Cursor supports four kinds:
- Project rules in
.cursor/rules/: version-controlled with your code and scoped to the repo. This is where .NET conventions belong. - User rules: global to your Cursor install, used by Agent. Good for personal preferences (British spelling, terse answers), not for architecture.
- Team rules: managed from the Cursor dashboard on Team and Enterprise plans.
AGENTS.md: plain Markdown instructions at the repo root (and optionally in subfolders), a simpler alternative to.cursor/rules.
The anatomy of an .mdc rule
A project rule is an .mdc file (name it anything) with YAML frontmatter. The extension matters: a plain .md file in .cursor/rules/ is ignored by the rules system because it has no frontmatter. Three fields decide when the rule is included:
| Rule type | Frontmatter | When it's included |
|---|---|---|
| Always Apply | alwaysApply: true | Every chat session; globs and description are ignored |
| Apply to Specific Files | alwaysApply: false + globs | Auto-attached when a matching file is in context |
| Apply Intelligently | alwaysApply: false + description, no globs | When the agent judges the description relevant to the task |
| Apply Manually | neither globs nor description | Only when you @-mention it in chat |
A file-scoped rule for EF Core data access looks like this. Multiple glob patterns are comma-separated:
---
description: EF Core read and query conventions
globs: src/Infrastructure/**/*.cs, **/Repositories/*.cs
alwaysApply: false
---
- Read-only queries use AsNoTracking() or a projection with Select().
- No Include() chains deeper than two levels on list endpoints.
- Never return IQueryable from a repository; materialise inside Infrastructure.
- Before writing a query, check docs/schema/schema.sql for real column names.
One detail people miss: a file mention inside a rule (for example @docs/schema/schema.sql) tells the agent where to look; the agent reads the file with its tools when needed, and the contents are not inlined. Anything that must always be in context has to be in the rule body itself.
A rule layout for a layered .NET solution
For .NET you want separate rules for DI lifetimes, Result<T> discipline, EF reads and layer boundaries. One monolithic "be a good architect" rule does not survive a real MediatR pipeline, and an always-on 400-line rule costs tokens on every prompt while burying the instruction that matters. A layout that maps onto Clean Architecture:
| Rule | Type | Scope | What it enforces |
|---|---|---|---|
arch-core.mdc | Always Apply | everything | Layer boundaries, build/test expectations, where the decision log lives. Keep it short. |
domain.mdc | Specific files | src/Domain/**/*.cs | No EF Core, HttpClient or IConfiguration; value objects; Result<T> for expected failures |
ef-core-reads.mdc | Specific files | src/Infrastructure/**/*.cs | AsNoTracking on reads, no N+1, bounded Include depth, schema digest |
di-scoping.mdc | Specific files | **/Program.cs, **/*ServiceCollection*.cs | No Scoped service captured by a Singleton; DbContext stays Scoped |
api-conventions.mdc | Specific files | src/Api/**/*.cs | Map Result<T> to ProblemDetails; no DbContext in endpoints |
tests.mdc | Specific files | tests/**/*.cs | Arrange-Act-Assert, builders over magic values, real database for integration tests |
migration-checklist.mdc | Manual | @migration-checklist | Step-by-step playbook for schema changes, invoked when needed |
Globs are matched against paths from the repository root, so check them against your actual folder names (src/ or not, Infrastructure or Persistence). A rule whose glob never matches simply never loads, and it fails silently.
Migrating from .cursorrules
The single .cursorrules file at the repo root is the original format. Cursor's docs now call it legacy and say it will be deprecated. It still works for the moment, with one gotcha: if you also have .mdc rules, they take precedence where the two overlap, which is why a .cursorrules file can look "ignored" in a repo that has started using .cursor/rules. Cursor's own migration path:
- Open the command palette and run New Cursor Rule (or type
/create-rulein Agent). - Paste the contents of
.cursorrulesinto the new rule. - Set the rule type to Always Apply, which matches the old behaviour.
- Delete
.cursorrulesand commit both changes together.
Then take the step that actually improves results: split the migrated rule. Anything that only matters for one layer moves into a glob-scoped rule from the table above, and the Always Apply rule shrinks to the handful of lines that genuinely apply everywhere.
AGENTS.md or .cursor/rules?
AGENTS.md is plain Markdown, no frontmatter, picked up automatically from the project root. It's a good fit for a short always-on brief (how to build, how to run tests, which folders are generated) and it's a convention several agent tools read, which helps if your team mixes tools. When you need scoping, choose .cursor/rules: AGENTS.md can't say "only for files under src/Infrastructure". Many .NET repos end up with both: a 20-line AGENTS.md and a handful of scoped .mdc rules.
Architectural persistence: the LEARNING_LOG
Chat history is not memory. Persistence means a committed LEARNING_LOG.md plus a rule (persistence.mdc) that tells the agent to read the log at session start and append ADR-style entries when you make non-obvious calls.
Example entry after you reject a suggestion:
## ADR-009 — Order queries
- Read paths use AsNoTracking via IOrderReadRepository.
- No Include deeper than 2 levels on list endpoints.
Next morning, new chat, same repo: the model reads entry #9 before touching OrderRepository.cs. Rules say how code should look; the log says which decisions are settled and why, which is what stops an agent from "helpfully" undoing them.
Circuit breaker for hallucination loops
When Cursor invents APIs that do not exist, most engineers become the circuit breaker manually. bug-breaker.mdc automates that: after repeated failed edits on one file, generation stops and the agent must re-read real sources. The full pattern is in Cursor keeps inventing .NET methods.
Why rules get ignored: five checks
- Wrong extension.
.mdfiles in.cursor/rules/are ignored; rename to.mdc. - No trigger. A rule with neither
globsnordescription(andalwaysApply: false) only applies when you@-mention it. - Glob mismatch. The pattern doesn't match the file's path from the repo root.
- Buried instruction. The rule is so long that the line you care about is lost. One concern per rule.
- Expecting file contents. An
@filereference in a rule points the agent at a file; it doesn't paste it in.
FAQ
Is .cursorrules deprecated?
Cursor's documentation calls the root .cursorrules file legacy and says it will be deprecated. It still works for now, but the supported format is .mdc files in .cursor/rules/. To migrate, create a new rule, paste the old content, set it to Always Apply, and delete .cursorrules.
Where do Cursor project rules go?
In the .cursor/rules/ directory of your repository, as .mdc files with YAML frontmatter (description, globs, alwaysApply). Plain .md files in that directory are ignored by the rules system. Commit them so the whole team and every agent session gets the same rules.
What is the difference between AGENTS.md and .cursor/rules?
AGENTS.md is plain Markdown at the repo root (and optionally in subdirectories) for simple instructions that should always apply. Rules in .cursor/rules/ add control: they can be scoped to file patterns with globs, pulled in by description, or invoked manually. Use AGENTS.md for a short always-on brief and .mdc rules for layer-specific guidance.
Why is Cursor ignoring my rule?
The usual causes are a .md instead of .mdc extension, a rule with neither globs nor a description (which only applies when you @-mention it), a glob that doesn't match the file's path from the repo root, or an always-on rule so long that the important instruction is buried.
Related essays (free)
- The Context Tax
- Stop Cursor AI drift on .NET
- Stop Cursor forgetting your database schema
- EF Core patterns with Cursor
- Cursor for C# and .NET: extensions, debugging and Unity
- Cursor vs Rider for .NET
Get the full rule pack
Nine scoped .mdc rules, templates, hydrate prompt, Quickstart. £9.00 one-time via Payhip. Lifetime updates. 30-day refund. MIT for team repos.
Free starter (3 rules)
result-pattern, di-scoping, ef-core-reads via email signup.