Skip to content
On this page

C# Records

csharp

Generate C# record types from SQL schema

Example Output ​

Generated from the pet-store sample schema:

csharp
namespace Generated;

public record Adoption(

    int Id,
    int PetId,
    int OwnerId,
    DateTime AdoptedAt,
    decimal AdoptionFee

);

public record AdoptionsAuditLog(

    long Id,
    string TableName,
    string Operation,
    string? OldData,
    string? NewData,
    DateTimeOffset ChangedAt

);

public record Category(

    int Id,
    string Name,
    string? Description

);

public record LegacyInventory(

    int Id,
    string? ItemName,
    string? OldSku,
    int? Quantity

);

public record Location(

    int Id,
    string Name,
    string Address,
    string City,
    string? Zip

);

public record MedicalRecord(

    int Id,
    int PetId,
    DateOnly VisitDate,
    string Diagnosis,
    string? Treatment,
    string? VetName

);

public record Owner(

    int Id,
    string Name,
    string Email,
    string? Phone,
    DateTime? CreatedAt

);

public record Pet(

    int Id,
    int? CategoryId,
    string Name,
    string Sku,
    decimal Price,
    string? InternalNotes,
    string Status,
    DateTime? CreatedAt

);

public record Review(

    int Id,
    int PetId,
    int OwnerId,
    int Rating,
    string? Body,
    int? LocationId,
    DateTime? CreatedAt

);

public record StaffAuditLog(

    long Id,
    string TableName,
    string Operation,
    string? OldData,
    string? NewData,
    DateTimeOffset ChangedAt

);

Usage ​

Add to your sqldoc.config.ts:

typescript
export default {
  dialect: 'postgres',
  schema: ['./schema.sql'],
  codegen: [
    {
      template: 'csharp-records',
      output: './generated/Models.cs',
    },
  ],
}

Then run:

bash
sqldoc codegen