⇄ Code & API Conversion

C# Class to SQL Table Generator

Generate CREATE TABLE statements from C# classes for SQL Server, MySQL, and PostgreSQL.

Updated

T-SQL, MySQL, PostgreSQL PK from Id or [Key] Nullable-aware Data annotations

Loading C# → SQL…

What this page sends

  • Your input: Processed in this tab and not sent to a server.
  • Share links: Share links put a Base64 copy of your input and output in the URL itself (after #d=). Anyone with the link can read it, so don't share a link that contains secrets.
  • Editor: On desktop screens the code editor (Monaco) is downloaded from cdn.jsdelivr.net; phones get a built-in lightweight editor instead.
  • Page load: Loading the page requests HTML, scripts and images from ByteKiln, fonts from Google Fonts, and sends Google Analytics page views, tool-usage events and catalog interactions (tool/category IDs, result status and whether a click followed search — never your input, output or search terms). Privacy & sharing

How the C# Class to SQL Table Generator Works

The generator parses your classes, records and enums with a hand-written C# declaration parser — not Roslyn — and turns each class with public properties into one CREATE TABLE statement for the dialect you pick. Each property is mapped through a per-dialect type table, data annotations adjust the name, type, length and nullability, and anything that cannot be a single column (collections, dictionaries, navigation properties) is skipped with a warning under the output rather than silently dropped. Enums are stored as their underlying integer type, as EF Core does by default. The CREATE TABLE statements are generated in your browser.

How are .NET types mapped?

string is NVARCHAR(255) on SQL Server, TEXT on PostgreSQL and VARCHAR(255) on MySQL unless [MaxLength] or [StringLength] gives a length. decimal is DECIMAL(18,2) (NUMERIC on PostgreSQL) unless [Precision] or [Column(TypeName)] says otherwise. DateTime is DATETIME2, TIMESTAMP or DATETIME; Guid is UNIQUEIDENTIFIER, UUID or CHAR(36); byte[] is VARBINARY(MAX), BYTEA or LONGBLOB.

Which property becomes the primary key?

[Key] first, then Id, then <ClassName>Id — the same conventions EF Core uses. Integer keys get the dialect's identity syntax: IDENTITY(1,1), GENERATED BY DEFAULT AS IDENTITY or AUTO_INCREMENT. Composite keys are not inferred.

What happens to navigation properties?

A property whose type is another class, such as public Customer Customer, is skipped with a warning. Its foreign key column (CustomerId) is kept as an ordinary column, but no FOREIGN KEY constraint or index is written — add those yourself.

Working with the C# → SQL

Why not just use EF migrations?

If the project already uses EF Core and the database is yours, use migrations. dotnet ef migrations add reads your DbContext, including Fluent API configuration, owned types, relationships and indexes, and it keeps a history you can roll back. This tool can't see any of that.

Where it earns its place is everywhere migrations don't reach: a DBA who wants a CREATE TABLE script to review before anything touches production, a Dapper or ADO.NET project with no DbContext, a quick SQLite or PostgreSQL table for a spike, or checking what types a DTO would need before you design the real schema. Paste the class, read the DDL, and edit it — it's a starting script, not a migration.

What does the sample produce?

The prefilled Order class covers the cases that usually go wrong. [Precision(10, 2)] narrows Total to DECIMAL(10,2) while Discount keeps the (18,2) default; DateTime? ShippedAt becomes a NULL column; the OrderStatus enum becomes INT; CustomerId stays as a column while the Customer navigation property is skipped with a warning.

SQL Server output (Order table)
CREATE TABLE [orders] (
  [Id] INT IDENTITY(1,1) PRIMARY KEY,
  [Reference] NVARCHAR(32) NOT NULL,
  [Notes] NVARCHAR(255) NULL,
  [Total] DECIMAL(10,2) NOT NULL,
  [Discount] DECIMAL(18,2) NULL,
  [CreatedAt] DATETIME2 NOT NULL,
  [ShippedAt] DATETIME2 NULL,
  [Status] INT NOT NULL,
  [CustomerId] UNIQUEIDENTIFIER NOT NULL
);

Limitations

  • It reads declarations only, with a regex-assisted parser rather than Roslyn: no Fluent API, no IEntityTypeConfiguration, no base-class properties from files you didn't paste.
  • String length is a guess unless you annotate it — 255 on SQL Server and MySQL, unbounded TEXT on PostgreSQL. Decimal defaults to (18,2), which truncates money stored with four decimals.
  • No FOREIGN KEY constraints, indexes, unique constraints, composite keys or check constraints are generated, and table names are not pluralized the way a DbSet<Customer> Customers would be.
  • DateTime maps to PostgreSQL TIMESTAMP (without time zone); Npgsql 6+ maps DateTime to timestamptz by default, so adjust if you mix this script with EF.
  • Collections, dictionaries and generic wrappers such as Result<T> are skipped, not turned into join tables or JSON columns.

FAQ

Short answers for the things developers usually ask before trusting a tool.

Which SQL dialects does the ByteKiln C# to SQL generator support?

SQL Server (T-SQL), PostgreSQL and MySQL. Identifiers are quoted per dialect ([brackets], "double quotes" or `backticks`) and each has its own type map — a string without a length is NVARCHAR(255) on SQL Server, TEXT on PostgreSQL and VARCHAR(255) on MySQL.

How are primary keys inferred from C# classes?

A property with [Key] wins; otherwise a property named Id, then one named <ClassName>Id, becomes the primary key. int and long keys get IDENTITY(1,1), GENERATED BY DEFAULT AS IDENTITY or AUTO_INCREMENT; a Guid key is a plain UNIQUEIDENTIFIER, UUID or CHAR(36) key with no database default, because EF Core generates Guid keys on the client.

Does it support nullable properties?

Yes, following nullable reference types: int?, DateTime? and string? become NULL, while int, DateTime and string (without ?) become NOT NULL. [Required] forces NOT NULL even on a nullable type. If your project has nullable reference types turned off, add ? to the strings that may be null.

Is my C# code uploaded to a server?

No. The class is parsed and the CREATE TABLE generated in your browser; it is not sent to a ByteKiln server. If you press Share, the link itself carries a readable copy of your input, so don't share links that contain secrets.

Which Entity Framework data annotations are read?

[Table("name")], [Key], [Column("name")], [Column(TypeName = "…")], [MaxLength(n)], [StringLength(n)], [Precision(p, s)], [Required] and [DefaultValue(x)]. [ForeignKey], [Index], [NotMapped] and Fluent API configuration in OnModelCreating are not read, because the tool only sees the class you paste.

Related tools

Useful follow-ups when one conversion usually turns into three more.

Want the background and worked examples? There's a longer write-up.

Read the C# → SQL guide

Related guides