Craticula

Entity Framework Core and Dapper

This page covers the two optional packages that run Craticula queries on a relational database: Craticula.Dapper and Craticula.EntityFrameworkCore. It is for a developer who has a SQL database and wants Craticula to run the query as well as write it.

Why adapters exist #

The core packages only compile. They never open a connection and never run anything. An adapter closes that gap for one data access library. It compiles the request, runs it, and gives you rows.

Adapters are optional. The core packages never depend on them. You can skip them and run the compiled statement yourself with any library you like.

Install them from NuGet: dotnet add package Craticula.Dapper or dotnet add package Craticula.EntityFrameworkCore. See the C# quickstart.

Dapper #

Craticula.Dapper compiles with Craticula.Sql and runs the statement on a connection you hand in. Use it when you want to see and control the SQL.

DapperGridPage result = await DapperGridQuery.FetchPageAsync(
    openConnection,
    new SqlQueryRequest { Schema = schema, Filter = filter, Sort = sort, Page = page },
    PostgresDialect.Instance,
    includeRowCount: true);

What it gives you:

What it needs from you:

Grouped requests work through the same method. The SQL compiler already writes the grouped statement, so the adapter just runs it.

Entity Framework Core #

Craticula.EntityFrameworkCore does not write SQL text. It builds IQueryable<T> expression trees, and the EF Core provider turns them into SQL. Use it when you already have a DbContext and want its global query filters, such as a tenant filter, to apply.

var request = new EfCoreQueryRequest { Schema = schema, Filter = filter, Sort = sort, Page = page };

EfCoreGridPage result = await EfCoreGridQuery.FetchPageAsync(
    source, request, EfCoreProviderSemantics.Sqlite, includeRowCount: true);

What it gives you:

What it needs from you:

The fields of the schema must use a plain physical.sql.column. A field with qualifiedColumn or expression is refused, because neither has a form in an expression tree.

If one DbContext type can be built from more than one schema, register GridSchemaModelCacheKeyFactory and implement IGridSchemaModel on the context. Without that, EF reuses the first schema's model for the next schema and fails with errors that do not mention caching.

Two silent traps #

Both of these give wrong rows without an error. The adapter cannot see your model or your collations, so it cannot guard against them.

Date and time cells leave a page or an export as UTC values whatever the provider returned.

What both adapters share #

What they never do #

Which one to pick #

You haveUse
A plain IDbConnection and want to see the SQLDapper
Any of the five SQL dialects, with no extra codeDapper
A DbContext with global query filters you want to keepEntity Framework Core

The demo runs both on PostgreSQL, so you can compare them on the same data.

Where this comes from #