NOTE: Not affiliated in anyway with Timescale DB nor is this super well made, but works for most cases.
- Add
EFCore.TimescaleDB.Extensions
reference to your project. - Add the following attribute to your startup/data project:
[assembly: DesignTimeServicesReference("EFCore.TimescaleDB.Extensions.DesignTimeServices, EFCore.TimescaleDB.Extensions")]
- Use the
.ConfigureTimescale()
extension of theDbContextOptionsBuilder
, e.g.:
var host = Host.CreateDefaultBuilder(args)
.ConfigureServices(
services =>
{
services.AddDbContext<ApplicationDbContext>(
options =>
options.UseNpgsql("<connection-string>")
.ConfigureTimescale());
}).Build();
- Use the
IsHyperTable(...)
extension of theEntityTypeBuilder
to select the entities which should be configured:
[Keyless]
public class Product
{
public string Name { get; set; }
public DateTimeOffset Time { get; set; }
}
public class ApplicationDbContext : DbContext
{
public DbSet<Product> Products { get; set; }
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
{ }
protected override void OnModelCreating(ModelBuilder modelBuilder)
=> modelBuilder.Entity<Product>().IsHyperTable(nameof(Product.Time, retentionInterval: "24 hours", chunkSize: "1 day"); // Use optional intervals
}
- OR: Add support for configuring via the HyperTable attribute/convention by using the
AddHyperTableConfiguration()
extension onModelConfigurationBuilder
[Keyless]
[HyperTable(nameof(Time), "24 hours", "1 day")] // Optional intervals
public class Product
{
public string Name { get; set; }
public DateTimeOffset Time { get; set; }
}
public class ApplicationDbContext : DbContext
{
public DbSet<Product> Products { get; set; }
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
{ }
protected override void ConfigureConventions(ModelConfigurationBuilder configurationBuilder)
=> configurationBuilder.AddHyperTableConfiguration();
}
-
Create a migration and update the database.
-
Due to the way HyperTables works, there will be no Down command generated. To use those, either rewrite the down method. In the retention case I have added the command required as comment which can be used, but needs the table to still exist.