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.1.82
- channel
- stable
- runtime
- C#
- license
- MIT-NC
Available on
# NuGet
dotnet add package Socigy.OpenSource.DBDocumentation
7 chapters01
Getting started
Install the package, annotate your first table, run a typed INSERT and SELECT, and configure the project.
InstallationQuickstartProject structureConfiguration
02Core concepts
How the Roslyn source generator works, how connections and DI are managed, and the transaction model.
How it worksConnections & DITransactions
03Defining 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
04Querying
Build every kind of SQL statement — reads, writes, joins, and set operations — with the generated fluent API.
SELECTWHERE expressionsSorting & paginationINSERT
05Advanced
Value convertors, procedure mapping from SQL files, DB constants, and the type-safe CHECK constraint DSL.
Value convertorsProcedure mappingDB constantsCheck constraint DSL
06Migration
Generate DDL from annotated C# assemblies, apply schema changes, and hook in custom migration logic.
ConfigurationCLI toolSchema generationApplying migrations
07Integrations
Plug the package into ASP.NET Core via generated DI extensions, or manage connections manually.
ASP.NET CoreManual connections
Quickstart
From zero to a typed query in minutes.
Follow the steps to get DB wired up. Everything you need is in one package.
- 01Add the NuGet package.
dotnet add package Socigy.OpenSource.DB - 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; } } - 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; }
}