Skip to content

Base DbContext

BaseDbContext removes repeated model conversion setup. Use it with Arc’s context registration helpers so entity-map discovery and application services are also available.

Model declarations (supply your application’s Customer entity):

using Cratis.Arc.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
public class StoreDbContext(DbContextOptions<StoreDbContext> options) : BaseDbContext(options)
{
public DbSet<Customer> Customers => Set<Customer>();
}

Registration fragment in an existing host:

services.AddDbContextWithConnectionString<StoreDbContext>(
"Data Source=store.db",
(serviceProvider, options) => options.EnableDetailedErrors());

Import Microsoft.Extensions.DependencyInjection for service registration. The callback is Action<IServiceProvider, DbContextOptionsBuilder>.

During OnModelCreating, the base context applies:

Relevant types include owned entities, types directly exposed through DbSet<T>, and entity types referenced directly or through generic properties on those set types. Types stored exclusively inside JSON are excluded from relational concept/Guid mapping.

Geometry conversion is not automatic. Configure AsPoint(), AsLineString(), or AsPolygon() explicitly, or choose the separate [Json] path. See point conversion for the storage distinctions.

Entity maps are applied only when an IEntityTypeRegistrar is available through the options’ application service provider. Arc registration wires this up. Directly constructing a context, or ordinary EF registration without the corresponding services/options, is not equivalent.

Arc’s helpers register a pooled IDbContextFactory<T> and a scoped context. Configure options at registration time; do not change them in OnConfiguring on a pooled context. Contexts created directly through a factory must be disposed by their caller.

If you override OnModelCreating, call base.OnModelCreating(modelBuilder) to retain the conventions. Configuration ordering matters: configure explicit overrides after the base call where appropriate.

Pooling does not provide tenant isolation. The helpers capture a connection string rather than resolving one per request. See registration and provider limits.