Introduction
Entity Framework Core is Microsoft's ORM — map C# classes to SQL Server tables and query with LINQ instead of raw SQL. ShopNest Inventory starts here.
After this article you will
- Compare EF Core vs Dapper vs ADO.NET
- Install packages and configure DbContext
- Define DbSet entities for inventory
- Verify database connection on startup
Prerequisites
- Article 12 — Static Files, Bundling and Minification
- ShopNest.Web project from prior lessons
Concept deep-dive
| Approach | When |
|---|---|
| EF Core | Full CRUD apps, migrations, LINQ — default for ShopNest |
| Dapper | Micro-ORM, hand-tuned SQL, read-heavy reports |
| ADO.NET | Legacy, full control, no ORM overhead |
public class InventoryDbContext : DbContext
{
public InventoryDbContext(DbContextOptions<InventoryDbContext> options) : base(options) { }
public DbSet<Product> Products => Set<Product>();
public DbSet<Warehouse> Warehouses => Set<Warehouse>();
}
// Program.cs
builder.Services.AddDbContext<InventoryDbContext>(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));
Hands-on — ShopNest Inventory Management
- dotnet add package Microsoft.EntityFrameworkCore.SqlServer
- Connection string in appsettings.json
- Create Product entity and DbContext
- dotnet ef database update (after first migration in Article 14)
Common errors & best practices
- Connection string wrong — test with SSMS first.
- DbContext registered as Singleton — must be Scoped.
Interview questions
Q: What is DbContext?
A: Unit of work + session to database; tracks changes and SaveChanges persists.
Summary
- EF Core maps classes to tables via DbContext
- Use SQL Server provider for ShopNest inventory
- Choose ORM based on app complexity
Previous: Static Files, Bundling and Minification
Next: EF Core Code First
FAQ
Code First vs Database First?
Code First: C# models drive schema — ShopNest default. Database First: scaffold from existing DB.