Projecting the Data Layer in .NET with EF Core
Developers often integrate Entity Framework Core into .NET projects but make typical mistakes: incorrect DbContext configuration, missing global filters, forgetting AsNoTracking(). The result is slow queries and N+1 problems. We see this in every second project that comes in for audit. Correct EF Core setup from the start cuts development time by 30% and prevents 80% of performance issues.
Entity Framework Core is the main ORM for .NET. Unlike classic EF, it was written from scratch, supports PostgreSQL via Npgsql, and installs as a NuGet package. We have been using EF Core in production for over eight years and guarantee stable operation with ASP.NET Core. Setup includes choosing the right provider, configuring connection pooling, and setting timeouts.
Why Correct EF Core Configuration Is Critical
Incorrect DbContext or query configuration degrades performance. For example, missing AsNoTracking() on read‑only operations loads the change tracker, increasing response time by 3–5 times. Poorly designed indexes and missing global filters (e.g., soft delete) complicate the code. We solve these issues during setup: add global filters via HasQueryFilter, configure indexes via Fluent API, and use SplitQuery to eliminate Cartesian products.
How to Avoid N+1 Problems When Configuring EF Core
One client came with an e‑commerce project: the catalog page took 6 seconds to load. Analysis showed N+1 queries on every product card. We configured Include() and projections via Select(), added AsNoTracking(). After optimization, LCP dropped from 6.5 to 1.2 seconds. This is a typical example – according to the official EF Core documentation, ignoring AsNoTracking can slow queries tenfold. For complex scenarios, we use automatic navigation property inclusion via AutoInclude or explicit projections.
What Is Included in Turnkey EF Core Setup
- Data model and DbContext design
- Connection setup to PostgreSQL or another DBMS
- Entity configuration via
IEntityTypeConfiguration - Migration creation and management (Code First)
- LINQ query optimization: eliminating N+1, using
AsNoTracking, split queries - Database schema and API documentation
- Team training on EF Core
- 30 days of support after delivery
EF Core vs. Other ORMs
| Characteristic | EF Core 8 | Dapper | NHibernate |
|---|---|---|---|
| Read performance | 95% of Dapper | 100% (reference) | 70% |
| LINQ support | Full | None | Partial |
| Migrations | Code First / CLI | None | Fluent NHibernate |
| Setup time | 1 day | 2 hours | 2–3 days |
| Query flexibility | High | Medium (SQL) | High |
EF Core is the best choice for projects that actively use LINQ and migrations. Dapper is faster but requires manual SQL writing. NHibernate is outdated.
Step‑by‑Step Setup: Key Steps
-
Install packages and create DbContext. Install via NuGet:
Microsoft.EntityFrameworkCore,Npgsql.EntityFrameworkCore.PostgreSQL, andMicrosoft.EntityFrameworkCore.Design. Create anAppDbContextclass inheriting fromDbContextand defineDbSetfor each entity. In theOnModelCreatingmethod, apply configurations viaApplyConfigurationsFromAssembly. -
Register in DI and configure connection. Add
AddDbContextinProgram.cswith the connection string and parameters:UseNpgsql,CommandTimeout,EnableRetryOnFailure. This ensures resilience to transient faults. -
Create and apply migrations. Run
dotnet ef migrations add Initialanddotnet ef database update. For production, use the--idempotentflag.
Example minimal DbContext:
public class AppDbContext : DbContext { public AppDbContext(DbContextOptions<AppDbContext> options) : base(options) { } public DbSet<User> Users => Set<User>(); protected override void OnModelCreating(ModelBuilder mb) { mb.ApplyConfigurationsFromAssembly(typeof(AppDbContext).Assembly); mb.Entity<User>().HasQueryFilter(u => !u.IsDeleted); } } Example of diagnosing performance issues
For slow queries, use ToQueryString() to view generated SQL, and enable logging via LogTo(Console.WriteLine, LogLevel.Information). Analyzing PostgreSQL query plans with EXPLAIN ANALYZE helps identify missing indexes.
Typical Mistakes and How to Avoid Them
| Mistake | Cause | Solution |
|---|---|---|
| N+1 query | Lazy loading | Use Include or Projection |
| Slow queries | Missing indexes | Add indexes via Fluent API |
| Memory leak | AsNoTracking not used |
Apply for read‑only |
| Migration conflicts | Manual DB changes | Use --idempotent flag |
Results and Guarantees
After our setup you get:
- Page load time reduction of 40% (improved LCP)
- Guarantee of no N+1 problems
- Complete schema and API documentation
- Team training (2‑hour workshop)
- 30 days of free support
Contact us to evaluate your project and get a consultation on EF Core setup. Over eight years of experience and 50+ successful projects. Commission a setup now – it will cut your development costs by up to 30%.







