Entity Framework Core basics: models, DbContext and LINQ queries

Entity Framework (EF) is Microsoft’s object-relational mapper for .NET. You define plain C# classes, EF maps them to tables, and you query with LINQ, which EF translates into SQL. It also tracks changes to the objects it loaded and writes them back when you call SaveChanges.
There are two products with the name:
- Entity Framework 6 (EF6) is the original, built for .NET Framework. It still gets fixes, but new development happens in EF Core.
- Entity Framework Core (EF Core) is a cross-platform rewrite that runs on modern .NET. EF Core 10 (November 2025) runs on .NET 10, which is a long-term support release. EF Core 11 (November 2026) requires .NET 11.
EF isn’t part of the .NET runtime or the base class library. You install it from NuGet. This post covers EF Core.
How EF Core is put together
Section titled “How EF Core is put together”- Entity classes are ordinary C# classes. EF uses conventions to map them: a property named
Idor<Type>Idbecomes the primary key, and a property likeBlogIdalongside aBlognavigation becomes a foreign key. DbContextis a unit of work. It holds the connection configuration, exposes aDbSet<T>per table, tracks the entities it loaded, and saves changes in a transaction. Contexts are cheap to create and aren’t thread-safe, so use one per request or operation and dispose it afterwards.- The model is built at runtime from your classes, conventions, and anything you configure in
OnModelCreatingor with attributes. EF Core has no visual designer or.edmxfile, unlike EF6. - Database providers translate queries for a specific database. Microsoft ships providers for SQL Server, SQLite, Azure Cosmos DB and an in-memory store. Third-party providers cover PostgreSQL (Npgsql), MySQL and others.
- Tooling (
dotnet ef) creates and applies migrations and can scaffold entity classes from an existing database.
A runnable example
Section titled “A runnable example”This example uses SQLite so it runs anywhere without a database server. For SQL Server, install Microsoft.EntityFrameworkCore.SqlServer and call UseSqlServer(connectionString) instead of UseSqlite.
dotnet new console -n EfDemocd EfDemodotnet add package Microsoft.EntityFrameworkCore.SqliteProgram.cs:
using Microsoft.EntityFrameworkCore;
using var db = new BloggingContext();db.Database.EnsureCreated(); // creates the database and tables if missing (see the note on migrations below)
var blog = new Blog { Url = "https://example.com" };blog.Posts.Add(new Post { Title = "Hello EF Core", Content = "First post" });blog.Posts.Add(new Post { Title = "Migrations", Content = "Second post" });db.Blogs.Add(blog);db.SaveChanges(); // one transaction: inserts the blog, then both posts with the new BlogId
// Load blogs together with their postsvar blogs = db.Blogs .Include(b => b.Posts) .OrderBy(b => b.BlogId) .ToList();
foreach (var b in blogs){ Console.WriteLine($"Blog {b.BlogId}: {b.Url} ({b.Posts.Count} posts)"); foreach (var p in b.Posts) Console.WriteLine($" - {p.Title}");}
// LINQ is translated to SQL; only the Title column is fetchedvar query = db.Posts .Where(p => p.Title.Contains("EF")) .Select(p => p.Title);Console.WriteLine(query.ToQueryString());Console.WriteLine(string.Join(", ", query.ToList()));
// Change tracking: modify a loaded entity, then savevar first = db.Posts.First();first.Title = "Hello again";db.SaveChanges(); // issues an UPDATE for the Title column only
public class BloggingContext : DbContext{ public DbSet<Blog> Blogs => Set<Blog>(); public DbSet<Post> Posts => Set<Post>();
protected override void OnConfiguring(DbContextOptionsBuilder options) => options.UseSqlite("Data Source=blogging.db");}
public class Blog{ public int BlogId { get; set; } public required string Url { get; set; } public List<Post> Posts { get; } = new();}
public class Post{ public int PostId { get; set; } public required string Title { get; set; } public required string Content { get; set; }
public int BlogId { get; set; } public Blog Blog { get; set; } = null!;}On the first run, the query part prints:
Blog 1: https://example.com (2 posts) - Hello EF Core - MigrationsSELECT "p"."Title"FROM "Posts" AS "p"WHERE instr("p"."Title", 'EF') > 0Hello EF CoreToQueryString() is a handy way to check what SQL a LINQ query produces. It’s provider-specific: SQLite uses instr for Contains, and SQL Server uses LIKE or CHARINDEX.
The required keyword and = null! keep the code free of nullable-reference warnings. EF Core reads nullability too: string columns become NOT NULL, and string? columns become nullable.
Migrations instead of EnsureCreated
Section titled “Migrations instead of EnsureCreated”EnsureCreated is fine for demos and tests, but it can’t change an existing schema. For a real application, use migrations so each model change becomes a versioned script:
dotnet tool install --global dotnet-efdotnet add package Microsoft.EntityFrameworkCore.Designdotnet ef migrations add InitialCreatedotnet ef database updateRemove the EnsureCreated call when you switch, because a database created by EnsureCreated can’t be updated by migrations later.
Gotchas
Section titled “Gotchas”- Related data isn’t loaded automatically. Without
Include,blog.Postsis empty unless those posts are already tracked by the context. Lazy loading exists, but it’s opt-in (theMicrosoft.EntityFrameworkCore.Proxiespackage) and can cause one query per row (the N+1 problem). - Read-only queries don’t need tracking. Add
.AsNoTracking()when you won’t modify the results. It’s faster and uses less memory. - Filter in the query, not after
ToList(). Everything beforeToList()runs in the database. Calling.ToList().Where(...)downloads the whole table first. - Configure through dependency injection in web apps. In ASP.NET Core, register the context with
builder.Services.AddDbContext<BloggingContext>(...)and inject it, rather than overridingOnConfiguring.
Further reading
Section titled “Further reading”© 2026 David Li · Home · Blog · RSS ·Classic site · Built with Astro, Starlight and three.js · Logos:Font Awesome Free (CC BY 4.0)