optimizing-ef-core-queries
यह क्या करती है
यह स्किल Entity Framework Core का उपयोग करने वाले डेटा एक्सेस code की समीक्षा करती है और उसे तेज़ करती है। Entity Framework .NET की database library है। यह उन .NET डेवलपर के लिए है जिनकी queries धीमी हैं, database calls बहुत ज़्यादा हैं या डेटा लोड memory ज़्यादा लेता है।
कब इस्तेमाल करें
- ✓जब Entity Framework queries धीमी हों या उम्मीद से कहीं ज़्यादा SQL भेजें।
- ✓जब हर row के लिए एक ही query चले, जिसे N+1 समस्या कहते हैं।
- ✓जब कई includes डुप्लिकेट rows दें या bulk updates धीमे हों।
Skill का लिंक
https://github.com/dotnet/skills/blob/main/plugins/dotnet-data/skills/optimizing-ef-core-queries/SKILL.mdइसे कॉपी करके अपने AI (Claude, ChatGPT या Cursor) में पेस्ट करें और निर्देशों का पालन करने को कहें।
कैसे इस्तेमाल करें
सबसे आसान तरीका: मैसेज कॉपी करके अपने AI में पेस्ट करें। वह skill पढ़कर निर्देशों का पालन करेगा।
पेस्ट करने के लिए तैयार मैसेज
Claude Code, Codex या Cursor के लिए: प्रोजेक्ट फ़ोल्डर में चलाएँ और skill इंस्टॉल हो जाएगी।
SKILL.md डाउनलोड करें और अपने एजेंट के skills फ़ोल्डर में रखें।
AI से क्या माँगें: उदाहरण
“मेरा EF Core सूची पेज सैकड़ों queries चलाता है। कारण ढूँढिए और उसे ठीक कीजिए।”
हर हफ़्ते 5 नई skills, 1 मिनट में पढ़ें।
यह कैसे काम करती है
एजेंट डेटा एक्सेस code को बिना चलाए ट्रेस करता है, पता लगाता है कि अतिरिक्त round trips या डुप्लिकेट rows कहाँ से आते हैं, और ऐसे बदलाव सुझाता है जो database का काम घटाएँ, उपलब्ध होने पर SQL या plans से जाँचकर।
तकनीकी सामग्री (मूल SKILL.md)
लेखक का मूल टेक्स्ट, अंग्रेज़ी में, सीधे GitHub से।
Optimizing EF Core Queries
Diagnose and fix slow or incorrect application data access built on EF Core. In a source-only review, trace the code without executing it and separate proven code behavior from runtime assumptions. When SQL, logs, or plans are available, use them to confirm the bottleneck. Prefer changes that reduce round-trips, duplicated rows, scans, or per-call translation cost over micro-optimizations.
When to Use
- EF Core queries are slow or emit far more SQL statements than expected
- The same query repeats once per row (N+1 / lazy loading)
- Multiple collection
Includes blow up or duplicate rows - Deep pages slow down as
Skipgrows, or bulk updates load rows just to modify them - A filtered/sorted query scans even though the column is indexed, or a filtered/sorted column has no supporting index
- A hot, frequently-executed query pays EF Core's LINQ-translation cost on every call
When Not to Use
- The code uses Dapper or raw ADO.NET, not EF Core. Answer the SQL/indexing/query-plan question directly; do not introduce a
DbContextor recommendAsNoTracking,Include,AsSplitQuery, or other EF Core APIs.
Source review workflow
- Find the EF boundary:
DbContext,DbSet, repository implementation, unit of work, query helper, or model-building code. Follow the implementation rather than assuming what an interface method does. - Trace each query to execution or enumeration. Check whether operators bind to
QueryableorEnumerable, including inside helpers; anIEnumerablecan remain a deferred database sequence. - Count database operations and actual transaction boundaries along the calling path, including immediate writes and repeated enumeration. Also track which operations share the same
DbContextinstance - For every execution path, independently inspect predicates, expression construction, projection contents, result bounds, ordering, Includes, tracking, SQL parameterization, and model-building code.
- Preserve the contract: result membership and order, random-page access, generated keys, retries, transaction and partial-failure behavior, concurrency handling, and domain/interceptor side effects.
- Report prioritized findings with locations, the affected call path, concrete impact, and a safe remedy. Briefly identify sound counterparts and state what was not validated. Do not equate source reasoning with measured plans or speedups.
For a source-only request, recommend runtime validation as a next step.
For raw SQL, prefer EF Core's interpolated overloads (FromSql / FromSqlInterpolated and ExecuteSql[Async]) so values are sent as parameters.
Gate source-only findings on evidence
When the user requests a source-only review, distinguish a demonstrated defect from an optimization that still needs workload or deployment evidence:
- A load-mutate loop followed by one
SaveChanges[Async]is notSaveChangesin a loop. Do not call it a per-row round-trip defect; suggest a bulk API only when the operation is demonstrably large and bypassing tracking semantics is safe. - Missing
HasIndexcalls in one supplied file do not prove that the deployed database lacks indexes. Use relevant model-building code, migrations, or a query plan as evidence. Do not pad a source review with generic conditional index advice when that evidence is absent. - Missing
AsNoTrackingis actionable only on proven read-only entity results. Scalar-only projections have nothing to track, but an entity inside a DTO/record is still tracked by default. - If every reviewed pattern is sound, say that no actionable changes are needed rather than manufacturing a finding.
Capture SQL and query counts
Use supplied logs and plans first. For an executable investigation, enable command logging to verify query shape and count before and after a change:
optionsBuilder.LogTo(Console.WriteLine, LogLevel.Information);
// or set "Microsoft.EntityFrameworkCore.Database.Command": "Information" in appsettings.json
Tag a query with .TagWith("...") to find it in the log. Count how many statements a slow operation runs, and how many rows each returns, before and after each change.
Fixes
Trace materialization and result bounds
Materialization inside a helper is still an execution boundary. A list or array has already loaded rows; later reductions run in memory. An IEnumerable<T> can instead hide a deferred EF query: its Enumerable operators process rows locally
अक्सर पूछे जाने वाले सवाल
क्या यह Dapper के लिए काम करती है?
नहीं। Dapper या raw SQL वाले code के लिए यह EF Core सलाह के बिना query का सवाल जवाब देती है।
क्या यह code चलाती है?
स्रोत समीक्षा में यह code को बिना चलाए ट्रेस करती है, और उपलब्ध होने पर SQL logs या plans इस्तेमाल करती है।
क्या यह सिर्फ़ छोटे सुधार करती है?
नहीं। यह छोटे micro-optimizations के बजाय round trips, डुप्लिकेट rows या scans घटाने वाले बदलावों को पसंद करती है।
जानकारी
लेखक का मूल विवरण
Review and optimize application data access that uses Entity Framework Core (EF Core). Use for EF Core performance and correctness reviews of services, repositories, imports, and background jobs: slow or memory-heavy queries, excessive round-trips, hidden SaveChanges, premature materialization, dynamic query construction, result-graph tracking, bulk writes, DbContext lifetime or overlapping operations, search semantics, paging, raw SQL, and model/index coverage. Use whether or not EF Core owns the schema. For EF Core, not Dapper or raw ADO.NET.
repo: dotnet/skills
लोकप्रियता
- GitHub स्टार
- 5.8k
- रैंकिंग में स्थान
- #14572
- लाइसेंस
- MIT
- अपडेट की गई
- 2/11/2026
कमेंट
- …
इस repository की और skills
create-custom-agent
5.8kयह आपके एजेंट को VS Code के custom agent फ़ाइलें बनाने देती है, जिनके अपने tools, निर्देश और दूसरे agents को handoffs होते हैं।
create-skill
5.8kयह आपके एजेंट को सही नाम, साफ़ trigger विवरण और मानक के अनुसार SKILL.md फ़ाइल वाली नई agent skill बनाने में मदद करती है।
csharp-scripts
5.8kयह आपके एजेंट को .NET command line टूल से एक छोटा C# प्रोग्राम एकल फ़ाइल के रूप में चलाने देती है, पूरा प्रोजेक्ट बनाए बिना।
dotnet-pinvoke
5.8kयह आपके एजेंट को .NET से native C या C++ libraries सही तरीके से कॉल करने में मदद करती है, जिसमें signatures, string संभालना, memory और सुरक्षित handles शामिल हैं।
मिलती-जुलती skills
api-connector-builder
280kयह आपके एजेंट को मौजूदा pattern के हिसाब से नए API connections बनाने देता है। जब बिना कोई नया तरीका बनाए एक और integration जोड़ना हो, तब इसका इस्तेमाल करें।
api-design
280kयह आपके एजेंट को best practices के हिसाब से REST APIs डिज़ाइन करने में मदद करता है। यह तय करने में काम आता है कि APIs आपस में कैसे बात करें, errors कैसे दिखें, और data को कैसे व्यवस्थित करें।
backend-patterns
280kआपके एजेंट को server-side architecture patterns और database optimization में मदद करता है। server पर चलने वाला code बनाने या review करने के लिए उपयोगी है।