This project demonstrates how to use the Database-First approach with TimescaleDB using the CmdScale.EntityFrameworkCore.TimescaleDB package.
Warning
Currently the dotnet ef dbcontext scaffold command can't be tested because of an issue in efcore (see dotnet/efcore#37201).
TODO: Test as soon as there is a fix available
Ensure the following package is installed in your project:
CmdScale.EntityFrameworkCore.TimescaleDB.Design
Use the following command to scaffold the DbContext and entity classes from an existing TimescaleDB database:
dotnet ef dbcontext scaffold \
"Host=localhost;Database=cmdscale-ef-timescaledb;Username=timescale_admin;Password=R#!kro#GP43ra8Ae" \
CmdScale.EntityFrameworkCore.TimescaleDB.Design \
--output-dir Models \
--schema public \
--context-dir . \
--context MyTimescaleDbContext \
--project samples/Eftdb.Samples.DatabaseFirstThis command will:
- Generate entity models in the
Models/directory - Place the
MyTimescaleDbContextin the current directory - Use the specified connection string to connect to the TimescaleDB instance
Note: When scaffolding your
DbContext, use the--schemaflag to target only your data schema (usuallypublic). This prevents the tool from unintentionally including TimescaleDB's internal management tables from schemas like_timescaledb_internal.
samples/Eftdb.Samples.DatabaseFirst/
|
+-- Models/ # Auto-generated entity models
+-- MyTimescaleDbContext.cs # Auto-generated DbContext
-
A
docker-compose.ymlfile is available at the repository root to spin up a TimescaleDB container for local development:docker-compose up -d
-
Connection string settings should match the configuration in your
docker-compose.yml.