The official Entity Framework Core provider for ClickHouse, built on top of ClickHouse.Driver.
Detailed documentation is available on the ClickHouse website.
await using var ctx = new AnalyticsContext();
var topPages = await ctx.PageViews
.Where(v => v.Date >= new DateOnly(2024, 1, 1))
.GroupBy(v => v.Path)
.Select(g => new { Path = g.Key, Views = g.Count() })
.OrderByDescending(x => x.Views)
.Take(10)
.ToListAsync();
public class AnalyticsContext : DbContext
{
public DbSet<PageView> PageViews { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder.UseClickHouse("Host=localhost;Port=9000;Database=analytics");
}
public class PageView
{
public long Id { get; set; }
public string Path { get; set; }
public DateOnly Date { get; set; }
public string UserAgent { get; set; }
}| Category | ClickHouse Types | CLR Types |
|---|---|---|
| Integers | Int8/Int16/Int32/Int64, UInt8/UInt16/UInt32/UInt64 |
sbyte, short, int, long, byte, ushort, uint, ulong |
| Big integers | Int128, Int256, UInt128, UInt256 |
BigInteger |
| Floats | Float32, Float64, BFloat16 |
float, double |
| Decimals | Decimal(P,S), Decimal32(S), Decimal64(S), Decimal128(S), Decimal256(S) |
decimal or ClickHouseDecimal (use ClickHouseDecimal for Decimal128/256 to avoid .NET decimal overflow) |
| Bool | Bool |
bool |
| Strings | String, FixedString(N) |
string |
| Enums | Enum8(...), Enum16(...) |
string or C# enum |
| Date/time | Date, Date32, DateTime, DateTime64(P, 'TZ') |
DateOnly, DateTime |
| Time | Time, Time64(N) |
TimeSpan |
| UUID | UUID |
Guid |
| Network | IPv4, IPv6 |
IPAddress |
| Arrays | Array(T) |
T[] or List<T> |
| Maps | Map(K, V) |
Dictionary<K,V> |
| Tuples | Tuple(T1, ...) |
Tuple<...> or ValueTuple<...> |
| Variant | Variant(T1, T2, ...) |
object |
| Dynamic | Dynamic |
object |
| JSON | Json |
JsonNode or string |
| Geographic | Point, Ring, LineString, Polygon, MultiLineString, MultiPolygon, Geometry |
Tuple<double,double> and arrays thereof; object for Geometry |
| Wrappers | Nullable(T), LowCardinality(T) |
Unwrapped automatically |
This provider is in active development. It supports LINQ queries, inserts, table engine configuration, and migrations — you can define ClickHouse tables with engine-specific settings, create them via dotnet ef migrations or EnsureCreated, query with LINQ, and write data via SaveChanges.
Where, OrderBy, Take, Skip, Select, First, Single, Any, Count, Distinct, AsNoTracking
Contains over a captured collection (Where(x => ids.Contains(x.Id))) sends the whole collection as one Array(T) parameter: x.Id IN {ids:Array(Int64)}. With one parameter for each element, ClickHouse refuses a request with more than 1,000 fields (http_max_fields), so a list of about 1,000 values fails. The array has the store type of the column. Any IEnumerable<T> works, and a null collection matches no rows, as in EF Core. The provider sends one parameter for each element for a nullable column, for an element type with a value converter (for example an enum), and for element types that the driver does not format inside an array, such as TimeSpan. To get one parameter for each element, use EF.MultipleParameters(ids) for one query or UseParameterizedCollectionMode(ParameterTranslationMode.MultipleParameters) for all queries; EF.Constant(ids) writes the values into the SQL. One parameter value can have at most http_max_field_value_size bytes (128 KiB by default), and the driver sends parameters URL-encoded in the URL, so a comma counts as 3 bytes. That is about 10,000 Int64 values of 10 digits, 5,900 of 19 digits, or 2,900 GUIDs. A longer list fails with HTML Form Exception: Field value too long. To raise the limit, set http_max_field_value_size in the default profile of the server configuration and restart the server; the setting has no effect in the connection string. Add UseFormDataParameters=true to the connection string too, so that the driver sends the parameters in the request body: in the URL, http_max_uri_size (1 MiB by default) also applies.
Join (INNER JOIN), GroupJoin + SelectMany + DefaultIfEmpty (LEFT JOIN), SelectMany (CROSS JOIN), and self-joins. Joining a local sequence (int[], string[], byte[]) also works, as does Contains against a local collection (T[], List<T>, and the ICollection/IList/IReadOnlyList interfaces), which becomes an IN predicate.
ClickHouse returns column defaults (0, "") instead of NULL for unmatched LEFT JOIN rows unless join_use_nulls=1 is set. The provider adds this setting to each statement that it sends, so LEFT JOIN gives the .NET semantics you expect. It does not change the settings of a ClickHouseConnection or ClickHouseDataSource that you give to UseClickHouse, so a command that you create yourself on that connection does not get the setting. If the connection settings already set join_use_nulls, the provider keeps that value. To opt out, use DisableJoinNullSemantics():
optionsBuilder.UseClickHouse("Host=localhost", o => o.DisableJoinNullSemantics());Contains over an IQueryable (IN (SELECT …)), Any (EXISTS), All, correlated scalar subqueries in a projection, and subqueries in FROM.
ClickHouse returns NULL from a scalar subquery that matches no rows, where standard SQL COUNT returns 0. The provider wraps COUNT and SUM scalar subqueries in ifNull(…, 0) when the target CLR type is a non-nullable value type, so a customer with no orders projects 0 rather than throwing.
Concat (UNION ALL), Union (UNION DISTINCT), Intersect, and Except, including chained and nested combinations. ClickHouse leaves union_default_mode empty and rejects a bare UNION, so the provider always emits an explicit ALL or DISTINCT modifier.
GroupBy with Count, LongCount, Sum, Average, Min, Max — including HAVING (.Where() after .GroupBy()), multiple aggregates in a single projection, and OrderBy on aggregate results.
Contains, StartsWith, EndsWith, IndexOf, Replace, Substring, Trim/TrimStart/TrimEnd, ToLower, ToUpper, Length, IsNullOrEmpty, Concat (and + operator)
Math.Abs, Floor, Ceiling, Round, Truncate, Pow, Sqrt, Cbrt, Exp, Log, Log2, Log10, Sign, Sin, Cos, Tan, Asin, Acos, Atan, Atan2, RadiansToDegrees, DegreesToRadians, IsNaN, IsInfinity, IsFinite, IsPositiveInfinity, IsNegativeInfinity — with both Math and MathF overloads.
The ClickHouse toStartOf* family is exposed through EF.Functions, so you can bucket and truncate timestamps directly in queries, including in GROUP BY:
ToStartOfYear, ToStartOfQuarter, ToStartOfMonth, ToStartOfWeek (with an optional ClickHouse week mode), ToStartOfDay, ToStartOfHour, ToStartOfMinute, ToStartOfSecond, ToStartOfFiveMinutes, ToStartOfTenMinutes, ToStartOfFifteenMinutes, and the general ToStartOfInterval(source, value, unit).
// Truncate to the start of the month
var monthly = await ctx.Events
.Select(e => EF.Functions.ToStartOfMonth(e.Timestamp))
.ToListAsync();
// Bucket into 15-minute intervals and count per bucket
var buckets = await ctx.Events
.GroupBy(e => EF.Functions.ToStartOfInterval(e.Timestamp, 15, ClickHouseInterval.Minute))
.Select(g => new { Bucket = g.Key, Count = g.Count() })
.ToListAsync();ToStartOfInterval takes a ClickHouseInterval unit (Second, Minute, Hour, Day, Week, Month, Quarter, Year) — from the ClickHouse.EntityFrameworkCore.Metadata namespace — and emits toStartOfInterval(source, toInterval<unit>(value)). The unit must be an inline enum constant. The interval size and optional ToStartOfWeek mode may be literals or captured query parameters, but cannot depend on values from the current row.
Input and return types follow ClickHouse. The calendar buckets (ToStartOfYear/Quarter/Month/Week) return Date; ToStartOfDay and the hour/minute buckets return DateTime; ToStartOfSecond returns DateTime64. They all accept DateTime and DateTime64 columns, and the plain truncation functions also accept DateOnly (Date/Date32). ToStartOfInterval is the exception: older ClickHouse rejects a DateOnly (Date/Date32) source with Illegal type Date32 of 1st argument while recent versions accept it. Prefer a DateTime/DateTime64 column for interval bucketing.
Date range: those default result types (
Date,DateTime) only span 1970–2149/2106, so ClickHouse narrows values outside that window — a pre-1970 date is clamped to the epoch (calendar buckets) or wraps around (sub-day/interval buckets). From ClickHouse 26.11,ToStartOfIntervalon aDateTime64column returnsDateTime64and keeps the value. To preserve the full range, enableenable_extended_results_for_datetime_functionsfor your session — e.g. addset_enable_extended_results_for_datetime_functions=1to the connection string — which makes ClickHouse returnDate32/DateTime64instead.
ToStartOfWeek defaults to ClickHouse week mode 0 (Sunday-based); pass a mode to change it.
SaveChanges supports INSERT operations using the driver's native InsertBinaryAsync API — RowBinary encoding with GZip compression, far more efficient than parameterized SQL.
await using var ctx = new AnalyticsContext();
ctx.PageViews.Add(new PageView
{
Id = 1,
Path = "/home",
Date = new DateOnly(2024, 6, 15),
UserAgent = "Mozilla/5.0"
});
await ctx.SaveChangesAsync();Entities transition from Added to Unchanged after save, just like any other EF Core provider.
Batch size is configurable (default 1000) — controls how many entities are accumulated before flushing to ClickHouse:
optionsBuilder.UseClickHouse("Host=localhost", o => o.MaxBatchSize(5000));For high-throughput loads that don't need change tracking, use BulkInsertAsync:
var events = Enumerable.Range(0, 100_000)
.Select(i => new PageView { Id = i, Path = $"/page/{i}", Date = DateOnly.FromDateTime(DateTime.Today) });
long rowsInserted = await ctx.BulkInsertAsync(events);This calls InsertBinaryAsync directly, bypassing EF Core's change tracker entirely. Entities are not tracked after insert.
The provider supports ClickHouse's Json column type, mapping to System.Text.Json.Nodes.JsonNode or string.
using System.Text.Json.Nodes;
public class Event
{
public long Id { get; set; }
public JsonNode? Payload { get; set; }
}
// In OnModelCreating:
entity.Property(e => e.Payload).HasColumnType("Json");Reading and writing JSON works through both SaveChanges and BulkInsertAsync:
ctx.Events.Add(new Event
{
Id = 1,
Payload = JsonNode.Parse("""{"action": "click", "x": 100, "y": 200}""")
});
await ctx.SaveChangesAsync();
var ev = await ctx.Events.Where(e => e.Id == 1).SingleAsync();
string action = ev.Payload!["action"]!.GetValue<string>(); // "click"If you prefer working with raw JSON strings, map the property as string with a Json column type — the provider will store and retrieve the raw JSON string as-is:
public class Event
{
public long Id { get; set; }
public string? Payload { get; set; } // raw JSON string
}
entity.Property(e => e.Payload).HasColumnType("Json");Indexing a JsonNode property translates to ClickHouse's native dot and subscript syntax, so filters and projections run on the server:
// WHERE CAST(`e`.`payload`.`age` AS Int32) > 20
// AND CAST(`e`.`payload`.`username` AS String) = 'alice_dev'
var users = await ctx.Events
.Where(e => (int)e.Payload!["age"]! > 20
&& e.Payload!["username"]!.GetValue<string>() == "alice_dev")
.ToListAsync();Paths nest to any depth (e.Payload!["meta"]!["runtime"]!["cpu_limit"]!), and array subscripts work too — the provider converts the 0-based .NET index to ClickHouse's 1-based one, so ["orders"]![0] emits .orders[1].
Both .GetValue<T>() and an explicit cast emit a CAST(… AS <storeType>), with the store type taken from the target CLR type. Keys and array indices must be compile-time constants. A variable key does not translate: in a Where it throws, and it can only be client-evaluated in a final Select.
For JSON held in a String column (not the native Json type), use the EF.Functions helpers, which map to ClickHouse's simpleJSON* family:
var clicks = await ctx.Logs
.Where(l => EF.Functions.SimpleJsonExtractString(l.RawPayload, "action") == "click")
.ToListAsync();SimpleJsonExtractBool, SimpleJsonExtractFloat, SimpleJsonExtractInt, SimpleJsonExtractRaw, SimpleJsonExtractString, SimpleJsonExtractUInt, SimpleJsonHas.
Limitations:
- No owned entity mapping —
.ToJson()/StructuralJsonTypeMappingis not supported. JSON columns are opaqueJsonNodeorstringvalues. JsonElement/JsonDocumentnot supported — onlyJsonNodeandstringCLR types are mapped.- NULL semantics — ClickHouse's JSON type returns
{}(empty object) for NULL values rather than SQL NULL. A row inserted withData = nullwill read back as an emptyJsonNode, notnull. - Integer precision — ClickHouse JSON stores all integers as
Int64unless the path is typed otherwise. When inspecting a materializedJsonNodein memory, useGetValue<long>()rather thanGetValue<int>(). This does not apply to a translated path query, whereGetValue<int>()emits an explicitCAST(… AS Int32)that the server applies.
Configure ClickHouse table engines, ordering, partitioning, and more via EF Core's fluent API:
modelBuilder.Entity<SensorReading>(b =>
{
b.HasKey(e => e.Id);
b.Property(e => e.Temperature).HasCodec("Delta, ZSTD");
b.Property(e => e.Location).HasColumnComment("Installation site");
b.HasIndex(e => e.Timestamp)
.HasSkippingIndexType("minmax")
.HasGranularity(4);
b.ToTable("sensor_readings", t => t
.HasReplacingMergeTreeEngine("Version")
.WithOrderBy("Id", "Timestamp")
.WithPartitionBy("toYYYYMM(Timestamp)")
.WithPrimaryKey("Id")
.WithTtl("Timestamp + INTERVAL 1 YEAR")
.WithSetting("index_granularity", "4096"));
});Supported engines: MergeTree, ReplacingMergeTree, SummingMergeTree, AggregatingMergeTree, CollapsingMergeTree, VersionedCollapsingMergeTree, GraphiteMergeTree, Log, TinyLog, StripeLog, Memory
Column-level DDL: .HasCodec("Delta, ZSTD"), .HasColumnTtl("expr"), .HasColumnComment("text")
Data-skipping indices: .HasSkippingIndexType("minmax"), .HasGranularity(4), .HasSkippingIndexParams("100")
Engine settings: .WithSetting("index_granularity", "4096") — any ClickHouse setting as a key-value pair
Default behavior: If no engine is configured, the provider defaults to MergeTree with the EF primary key as ORDER BY.
Each MergeTree family engine builder has .WithReplication(). It changes the engine to its Replicated variant, for example ReplacingMergeTree to ReplicatedReplacingMergeTree. Rows that you write to one replica are then copied to the other replicas of the shard.
modelBuilder.Entity<SensorReading>(b =>
{
b.ToTable("sensor_readings", t => t
.HasReplacingMergeTreeEngine("Version")
.WithOrderBy("Id")
.WithReplication());
});.WithReplication()givesReplicatedReplacingMergeTree(`Version`). ClickHouse then uses the server settingsdefault_replica_path(by default/clickhouse/tables/{uuid}/{shard}) anddefault_replica_name(by default{replica}). ClickHouse accepts{uuid}only in anON CLUSTERquery or in a database with theReplicatedengine. Thus, with the default settings, use.WithReplication()without arguments only together withUseCluster(see below) or in aReplicateddatabase..WithReplication("/clickhouse/tables/{shard}/{database}/{table}", "{replica}")givesReplicatedReplacingMergeTree('/clickhouse/tables/{shard}/{database}/{table}', '{replica}', `Version`). Set both values or neither.
If you drop a table and then create it again with the same fixed path, the create fails with REPLICA_ALREADY_EXISTS until ClickHouse removes the old replica. By default, ClickHouse waits 8 minutes (database_atomic_delay_before_drop_table_sec) before it removes it. A path with {uuid} does not have this problem.
A Replicated table ignores an insert that has the same rows as a recent insert into the table (replicated_deduplication_window, by default the last 10,000 inserts). Thus, if two SaveChanges calls insert the same rows, the table keeps only the rows of the first call. A plain MergeTree table keeps both. To keep both in a Replicated table, add .WithSetting("replicated_deduplication_window", "0").
A Replicated engine copies rows, but it does not create the table on the other replicas. Each statement that creates, changes or drops a table must run on all hosts. There are two ways to do this:
-
ON CLUSTER. UseUseCluster. The provider then addsON CLUSTERto the DDL that migrations,EnsureCreated()andEnsureDeleted()generate:CREATE/DROP DATABASE,CREATE/DROP/RENAME TABLE, and eachALTER TABLE(columns, data-skipping indices and check constraints).optionsBuilder.UseClickHouse(connectionString, o => o.UseCluster("my_cluster"));
The cluster must be in the
remote_serversserver setting. You can also use a macro, for exampleUseCluster("{cluster}"). If a host is not available, ClickHouse waits up todistributed_ddl_task_timeout(180 seconds by default) and the migration then fails. -
A database with the
Replicatedengine. ClickHouse replicates the DDL itself and refusesON CLUSTER, so do not useUseCluster. UseUseReplicatedMigrationsHistoryTable()instead. In such a database, ClickHouse also refuses an explicit path and replica name, so use.WithReplication()without arguments.
The provider writes the applied migrations to the __EFMigrationsHistory table. By default, this table is a MergeTree table on the server of the connection, so each host has its own history. The two options change this:
- With
UseCluster, the history table isReplicatedMergeTree('/clickhouse/tables/{uuid}', '{shard}-{replica}'). Its path has no{shard}, so all hosts of all shards share one history. - With
UseReplicatedMigrationsHistoryTable(), the history table isReplicatedMergeTree()with the server default path and replica name. In aReplicateddatabase, ClickHouse refuses a replicated table that the shards share, so each shard has its own history. If the database has more than one shard, run the migrations through the hosts of one shard only. Through a host of another shard, the applied migrations look pending, and the provider tries to apply them again.
In both cases, the provider turns off insert deduplication for the history table, so a migration that you revert and apply again gets its history row back. A host sees a migration as applied when the history row has replicated to it. The provider has no migration lock, so run the migrations from one place at a time.
The options change only how the provider creates the history table. If the table already exists, the provider uses it as it is. To change an existing MergeTree history table to a replicated one before you add UseCluster, run these statements in the database, on the host that has the full history:
CREATE TABLE `__EFMigrationsHistory_new` ON CLUSTER 'my_cluster'
(
`MigrationId` String,
`ProductVersion` String
)
ENGINE = ReplicatedMergeTree('/clickhouse/tables/{uuid}', '{shard}-{replica}')
ORDER BY (`MigrationId`)
SETTINGS replicated_deduplication_window = 0, replicated_deduplication_window_for_async_inserts = 0;
INSERT INTO `__EFMigrationsHistory_new` SELECT `MigrationId`, `ProductVersion` FROM `__EFMigrationsHistory`;
DROP TABLE IF EXISTS `__EFMigrationsHistory` ON CLUSTER 'my_cluster';
RENAME TABLE `__EFMigrationsHistory_new` TO `__EFMigrationsHistory` ON CLUSTER 'my_cluster';To use other settings for the history table, derive from ClickHouseHistoryRepository, override ConfigureTable, and register your class with ReplaceService<IHistoryRepository, YourHistoryRepository>().
The provider does not create Distributed tables. On ClickHouse Cloud you do not need these settings: ClickHouse Cloud replicates DDL itself and stores MergeTree family tables as SharedMergeTree.
The provider supports dotnet ef migrations for creating and applying migrations:
dotnet ef migrations add InitialCreate
dotnet ef database updateEnsureCreated() / EnsureDeleted() also work for quick setup without migrations.
Supported migration operations:
- CREATE TABLE with full ENGINE clause (all engine types and their Replicated variants, ORDER BY, PARTITION BY, PRIMARY KEY, SAMPLE BY, TTL, SETTINGS, codecs, comments, data-skipping indices)
- ADD COLUMN, DROP COLUMN, MODIFY COLUMN, RENAME COLUMN, RENAME TABLE
- DROP TABLE, CREATE/DROP INDEX (data-skipping)
- Custom
ClickHouseCreateDatabaseOperation/ClickHouseDropDatabaseOperation ON CLUSTERon all of the above and on check constraints withUseCluster(see Replication and Clusters)
ClickHouse limitations reflected in migrations:
- ALTER TABLE cannot change engine, replication, ORDER BY, PARTITION BY, or other structural metadata. The provider throws
NotSupportedExceptionwith a clear message - Foreign keys, unique constraints, and sequences throw
NotSupportedException - Primary key add/drop is a no-op (ClickHouse PK is structural, not a constraint)
- Idempotent scripts (
--idempotent) are not supported (ClickHouse has no conditional SQL blocks) - Transactions are suppressed (ClickHouse does not support them)
- UPDATE / DELETE (ClickHouse mutations are async, not OLTP-compatible)
- Server-generated values — identity columns,
RETURNING, computed defaults read back after insert - Reverse engineering / scaffolding (
dotnet ef dbcontext scaffold) - Owned entities mapped to JSON (
.ToJson()) - Queries that EF Core lowers to
CROSS APPLY/OUTER APPLY, which ClickHouse has no equivalent for. This covers correlatedSelectManythat selects the outer element or entity,Takeinside a collection projection, and correlated collections over aUNIONsource. - Set operations after a client projection (for example
Union(...).FirstOrDefault()on a client-evaluated shape)
dotnet build
dotnet test # requires Docker (uses Testcontainers)Targets .NET 10.0, EF Core 10.