This guide covers the essential CRUD operations: Create, Read, Update, Delete, and Query. Follow along to build your first REDB-powered application.
Installation
Install REDB packages via NuGet. Choose your database provider:
# For PostgreSQL
dotnet add package redb.Postgres
# For SQL Server
dotnet add package redb.MSSql
# For Pro features (advanced queries, bulk operations)
dotnet add package redb.Postgres.Pro
dotnet add package redb.MSSql.ProConfiguration
Register REDB services in your Program.cs:
// Free version
services.AddRedb(options => options
.UsePostgres("Host=localhost;Database=mydb;Username=user;Password=pass"));
// Pro version with license
services.AddRedbPro(options => options
//.WithLicense(License)
.Configure(c =>
{
c.EavSaveStrategy = EavSaveStrategy.ChangeTracking;
//c.EnablePropsCache = false;
//c.PropsCacheMaxSize = 10000;
//c.PropsCacheTtl = TimeSpan.FromMinutes(60);
})
.UsePostgres("Host=localhost;Port=5432;Username=postgres;Password=***;Database=redb;Pooling=true;Include Error Detail=true;Options=-c jit=off")
//.UseMsSql("Server=localhost;Database=redb;User Id=sa;Password=***;TrustServerCertificate=true;Command Timeout=600;")
);Define Your Model
Create a Props class with the [RedbScheme] attribute. This is your business entity - no database tables to create, no migrations to write:
using redb.Core.Attributes;
[RedbScheme("Product catalog")]
public class ProductProps
{
public string Name { get; set; } = "";
public string? Description { get; set; }
public decimal Price { get; set; }
public string[] Tags { get; set; } = [];
public bool IsActive { get; set; } = true;
}Create - SaveAsync
Create and save a new object. The SaveAsync method returns the generated ID:
var product = new RedbObject<ProductProps>
{
name = "Widget Pro base name",
Props = new ProductProps
{
Name = "Widget Pro",
Description = "A professional widget",
Price = 99.99m,
Tags = ["electronics", "tools"],
IsActive = true
}
};
var id = await redb.SaveAsync(product);
// id now contains the generated object IDRead - LoadAsync
Load an object by its ID. Returns the complete object with all properties:
var loaded = await redb.LoadAsync<ProductProps>(id);
// Access properties
var name = loaded.Props.Name;
var price = loaded.Props.Price;
// or
if (loaded != null) {
var product = (ProductProps)loaded;
var name = product.Name;
var price = product.Price;
}Query - Where
Filter data using familiar LINQ syntax. All conditions compile to native SQL:
// Simple filter
var expensive = await redb.Query<ProductProps>()
.Where(p => p.Price > 75000m)
.Take(100)
.ToListAsync();
// AND condition
var filtered = await redb.Query<ProductProps>()
.Where(p => p.Price >= 30 && p.Price < 100)
.ToListAsync();
// OR condition
var mixed = await redb.Query<ProductProps>()
.Where(p => p.Price < 10 || p.Price > 90000m)
.ToListAsync();
// NOT equal
var notWidgets = await redb.Query<ProductProps>()
.Where(p => p.Name != "Widget")
.ToListAsync();Count - CountAsync
Get the count of objects matching your criteria:
// Total count
var totalCount = await redb.Query<ProductProps>().CountAsync();
// Filtered count
var activeCount = await redb.Query<ProductProps>()
.Where(p => p.IsActive)
.CountAsync();Delete - DeleteAsync
Remove an object by ID. The object is permanently deleted:
// Delete by ID
await redb.DeleteAsync(id);
// Verify deletion
var deleted = await redb.LoadAsync<ProductProps>(id);
// deleted will be null