Lessons in this course (15)
1. What an ORM is, and EF6 vs EF Core
An ORM (object-relational mapper) maps classes to tables and objects to rows, generating SQL so you work with C# objects and LINQ instead of DataReader loops. Entity Framework is Microsoft's ORM, in two generations:
| Entity Framework 6 (EF6) | EF Core | |
|---|---|---|
| Released | EF 1 (2008, .NET 3.5 SP1) → EF 4.1 Code First (2011) → EF6 (2013), 6.4/6.5 last | 2016 (1.0) → EF Core 10 (2025) |
| Runs on | .NET Framework (EF 6.3+ also on .NET Core 3.1+/.NET 5+) | .NET Core / .NET 5+ only |
| Status | Maintenance only (bug/security fixes) | Active — new features every year |
| Modelling | EDMX designer (Database First / Model First, XML model: CSDL + SSDL + MSL) or Code First | Code First only (plus reverse engineering with dotnet ef dbcontext scaffold) |
| Context API | DbContext (wrapper over the older ObjectContext) | DbContext only |
| Lazy loading | On by default (virtual navigation properties) | Off by default (opt-in proxies) |
| Performance | Slower | Much faster (compiled queries, batching, no-tracking, split queries) |
| Extras | Spatial, entity splitting, stored-proc mapping in EDMX | Global query filters, owned/complex types, JSON columns, bulk ExecuteUpdate/ExecuteDelete, temporal tables, interceptors, many providers (SQL Server, PostgreSQL, SQLite, Cosmos DB) |
EF6 Database First with EDMX (legacy apps): Add → ADO.NET Entity Data Model → "EF Designer from database" → pick tables/procedures → Visual Studio generates ShopModel.edmx (diagram) + T4 templates that generate entity classes and a ShopEntities : DbContext. Changing the database means "Update Model from Database" in the designer. The connection string contains metadata (metadata=res://*/ShopModel.csdl|...;provider=System.Data.SqlClient;provider connection string="...").
// EF6 (System.Data.Entity) — .NET Framework MVC 5 / Web Forms
using (var db = new ShopEntities())
{
var orders = db.Orders.Include(o => o.Customer).Where(o => o.Total > 1000).ToList(); // Include(lambda) needs System.Data.Entity
db.Orders.Add(new Order { CustomerId = 42, Total = 4500 });
db.SaveChanges();
}// EF Core (Microsoft.EntityFrameworkCore) — same idea, modern API
await using var db = new ShopDbContext(options);
var orders = await db.Orders.Include(o => o.Customer).Where(o => o.Total > 1000).AsNoTracking().ToListAsync();Migrating EF6 → EF Core: move EDMX models to code-first classes (scaffold from the database), replace System.Data.Entity namespaces, review lazy-loading assumptions (add explicit Include), re-test queries that EF Core translates differently (client evaluation is no longer silent), replace EF6-only features (EDMX stored-procedure mapping, entity splitting differences), and adopt EF Core migrations from a baseline.
Interview tip: "Code First vs Database First": Code First — classes are the source of truth and migrations create/alter the database; Database First — the existing database is the source of truth and the model is generated from it (EDMX in EF6, scaffolding in EF Core).