/DB
Socigy.OpenSource.DB

Annotate once. Query forever.

Socigy.OpenSource.DB is a Roslyn source generator that reads your annotated C# classes at build time and emits a fully typed PostgreSQL data layer — INSERT, SELECT, UPDATE, DELETE, JOINs, set operations, and migrations — without a single line of boilerplate. Define your schema in C#, build, and every query method is already there waiting for you.

version
v0.3.1
channel
stable
runtime
C#
license
MPL-2.0
Available on

Documentation

11 chapters
01
Getting started
Install the package, annotate your first table, run a typed INSERT and SELECT, and configure the project.
InstallationQuickstartProject structureConfiguration
02
Core concepts
How the Roslyn source generator works, how connections and DI are managed, and the transaction model.
How it worksConnections & DIDatabase context & unit of workTransactions
03
Defining models
Every attribute that shapes how a C# class maps to a PostgreSQL table — columns, types, defaults, constraints, and special column kinds.
Tables & classesColumns & typesDefault valuesConstraints
04
Querying
Build every kind of SQL statement — reads, writes, joins, and set operations — with the generated fluent API.
SELECTWHERE expressionsAggregates & scalarsSorting & pagination
05
Advanced
Value convertors, procedure mapping from SQL files, DB constants, the type-safe CHECK constraint DSL, and the build-time generator diagnostics.
Value convertorsProcedure mappingDB constantsCheck constraint DSL
06
Observability
Emit the executed SQL, parameters and execution duration to OpenTelemetry and ILogger with zero per-call wiring.
Diagnostics & OpenTelemetryLogging
07
Performance
How the library performs against Dapper and EF Core (including NativeAOT), and how to reproduce the benchmarks yourself.
Benchmarks
08
Migration
Generate DDL from annotated C# assemblies, apply schema changes, and hook in custom migration logic.
ConfigurationCLI toolSchema generationApplying migrations
09
Integrations
Plug the package into ASP.NET Core via generated DI extensions, or manage connections manually.
ASP.NET CoreManual connectionsHashiCorp Vault
10
Testing
Unit-test your data-access code with no database by mocking the generated context interfaces.
Unit testing
11
Changelog
Release history — every fix, addition, and breaking change by version.
Changelog
Quickstart

From zero to a typed query in minutes.

Follow the steps to get DB wired up. Everything you need is in one package.

  1. 01Add the NuGet package.
    dotnet add package Socigy.OpenSource.DB
  2. 02Annotate a C# class as a table. Make it partial so the generator can augment it.
    [Table("users")]
    public partial class User
    {
        [PrimaryKey, Default(DbDefaults.Guid.Random)]
        public Guid Id { get; set; }
    
        // C# initializer → DEFAULT 'anonymous' in DDL
        public string Username { get; set; } = "anonymous";
    
        [Default(DbDefaults.Time.Now)]
        public DateTime CreatedAt { get; set; }
    }
  3. 03Build once — the generator emits Insert(), Query(), Update(), and Delete() methods. Then use them.
    // INSERT — skip [Default] columns so the database generates them
    var user = new User { Username = "alice" };
    await user.Insert()
        .WithConnection(conn)
        .ExcludeAutoFields()
        .ExecuteAsync();
    
    // SELECT with a typed WHERE predicate
    await foreach (var u in User.Query(x => x.Username == "alice")
        .WithConnection(conn).ExecuteAsync())
    {
        Console.WriteLine($"{u.Id}: {u.Username}");
    }
using Socigy.OpenSource.DB.Core.Attributes;
using Socigy.OpenSource.DB.Core.Values;

[Table("users")]
public partial class User
{
    [PrimaryKey, Default(DbDefaults.Guid.Random)]
    public Guid Id { get; set; }

    public string Username { get; set; } = "anonymous";

    public string? Email { get; set; }

    [Default(DbDefaults.Time.Now)]
    public DateTime CreatedAt { get; set; }
}