C# VS2026
Lesson 26 of 30

Database Access with Entity Framework Core

C# in Visual Studio 2026 — a hands-on guide for developers at every level.

What is Entity Framework Core?

Entity Framework Core (EF Core) is Microsoft's official Object-Relational Mapper (ORM) for .NET. You define C# classes (entities), and EF Core handles generating SQL and querying the database — no hand-written SQL required for most operations.

Setting Up (SQLite)

// Install via NuGet:
// dotnet add package Microsoft.EntityFrameworkCore.Sqlite
// dotnet add package Microsoft.EntityFrameworkCore.Design

using Microsoft.EntityFrameworkCore;

public class Product
{
    public int    Id    { get; set; }
    public string Name  { get; set; } = "";
    public decimal Price { get; set; }
}

public class AppDbContext : DbContext
{
    public DbSet<Product> Products => Set<Product>();

    protected override void OnConfiguring(DbContextOptionsBuilder opt)
        => opt.UseSqlite("Data Source=shop.db");
}

CRUD Operations

await using var db = new AppDbContext();
await db.Database.EnsureCreatedAsync();

// Create
db.Products.Add(new Product { Name = "Laptop", Price = 999.99m });
await db.SaveChangesAsync();

// Read
var cheap = await db.Products
    .Where(p => p.Price < 500)
    .ToListAsync();

// Update
var product = await db.Products.FirstAsync();
product.Price = 899.99m;
await db.SaveChangesAsync();

// Delete
db.Products.Remove(product);
await db.SaveChangesAsync();