Skip to content

Latest commit

 

History

History
71 lines (46 loc) · 2.16 KB

File metadata and controls

71 lines (46 loc) · 2.16 KB

EF Core Database-First Example with TimescaleDB

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


Required NuGet Packages

Ensure the following package is installed in your project:

  • CmdScale.EntityFrameworkCore.TimescaleDB.Design

Scaffold DbContext and Models

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.DatabaseFirst

This command will:

  • Generate entity models in the Models/ directory
  • Place the MyTimescaleDbContext in the current directory
  • Use the specified connection string to connect to the TimescaleDB instance

Note: When scaffolding your DbContext, use the --schema flag to target only your data schema (usually public). This prevents the tool from unintentionally including TimescaleDB's internal management tables from schemas like _timescaledb_internal.


Project Structure

samples/Eftdb.Samples.DatabaseFirst/
|
+-- Models/                     # Auto-generated entity models
+-- MyTimescaleDbContext.cs     # Auto-generated DbContext

Docker

  • A docker-compose.yml file 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.


Resources