A small .NET 8 console application demonstrating the fundamentals of Entity Framework Core with a SQLite database using a code-first approach.
It is a learning/example project that shows how to:
- Define an entity using data annotations.
- Configure a
DbContextwith the EF Core Fluent API (table mapping, unique indexes, SQL default values). - Create a SQLite database, seed it with data, and query it back.
When run, the program:
- Deletes any existing
TestDatabase.dbfile so each run starts fresh. - Creates the database from the model via
EnsureCreated(). - Seeds three sample
Productrows (only if the table is empty). - Reads every product back and prints it to the console.
- Waits for a key press before exiting.
Example output:
Product ID = 1 Title=Product 1 Product 2 subtitle Date added=6/30/2026 12:21:56 PM
Product ID = 2 Title=Product 2 Product 2 subtitle Date added=6/30/2026 12:21:56 PM
Product ID = 3 Title=Product 3 Product 2 subtitle Date added=6/30/2026 12:21:56 PM
| File | Purpose |
|---|---|
program.cs |
Application entry point — creates the database, seeds data, and prints products. |
MyDbContext.cs |
The EF Core DbContext. Configures the SQLite connection and maps the Product entity (table name, unique index on title, CURRENT_TIMESTAMP default for created_at). |
Models/Product.cs |
The Product entity (id, title, sub_title, created_at) with [Key], [Required], and [MaxLength] data annotations. |
Databases.csproj |
Project file targeting net8.0 with the EF Core SQLite and Design package references. |
Product maps to a table named products and has the following columns:
| Column | Type | Notes |
|---|---|---|
id |
int |
Primary key. |
title |
string |
Required, max length 128, unique index. |
sub_title |
string |
Required, max length 256. |
created_at |
DateTime |
Required, defaults to SQL CURRENT_TIMESTAMP. |
Note:
MyDbContextmaps the entity with atestschema (ToTable("products", "test")). SQLite has no concept of schemas, so the EF Core SQLite provider simply ignores it — the table is created asproducts.
- .NET 8 SDK or later.
This project targets the .NET 8 LTS, which has native support for Windows, Linux, and macOS (both Intel and Apple Silicon).
Clone the repository and run the app from the project root:
dotnet runTo build without running:
dotnet buildThe first run creates a TestDatabase.db SQLite file in the working directory.
You can inspect it with any SQLite client, for example:
sqlite3 TestDatabase.db "SELECT * FROM products;"EF Core builds the model from MyDbContext and the Product entity:
- Connection —
MyDbContext.OnConfiguringusesoptionsBuilder.UseSqlite("Filename=TestDatabase.db"). - Mapping —
MyDbContext.OnModelCreatingmaps the table name, adds a unique index ontitle, and sets thecreated_atdefault toCURRENT_TIMESTAMP. - Schema creation —
EnsureCreated()creates the database and tables from the model (note: this is a quick way to get started but is not the same as using migrations).
- Replace
EnsureCreated()with EF Core migrations for versioned schema changes. - Add more entities and relationships.
- Add update/delete operations to complete the CRUD examples.