Migrating a database schema to a .NET application means writing C# classes that mirror your SQL tables. Doing this manually for dozens of tables is slow and error-prone. This guide shows how to convert SQL CREATE TABLE statements to C# classes — covering EF Core entities, POCOs, and records.
Three C# Output Formats
Depending on your project, you'll want different class styles:
1. Entity Framework Core Entities
Full EF Core entities with data annotations for ORM mapping:
[Table("products")]
public sealed class Products
{
[Key]
public int Id { get; set; }
[Required]
[MaxLength(200)]
public string Name { get; set; }
[Column(TypeName = "decimal(10,2)")]
public decimal Price { get; set; }
public string? Description { get; set; }
[Required]
public bool IsActive { get; set; }
}C#
2. POCO Classes
Plain classes without ORM attributes — good for DTOs and clean architecture:
public sealed class Products
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
public string? Description { get; set; }
public bool IsActive { get; set; }
}C#
3. Records (Immutable)
C# records for read-only data transfer:
public sealed record Products(
int Id,
string Name,
decimal Price,
string? Description,
bool IsActive
);C#
SQL to C# Type Mapping
Getting the type mapping right is critical. Here's the complete reference:
| SQL Type | C# Type | Notes |
|---|---|---|
| INT | int | 32-bit integer |
| BIGINT | long | 64-bit integer |
| BIT | bool | true/false |
| VARCHAR / NVARCHAR | string | Use MaxLength for NVARCHAR(n) |
| DECIMAL(p,s) | decimal | Preserves precision |
| DATETIME / DATETIME2 | DateTime | Use DateTimeOffset for timezone |
| UNIQUEIDENTIFIER | Guid | UUID type |
| VARBINARY | byte[] | Binary data |
Always mark nullable SQL columns with ? in C# (e.g.,
string?, int?). This prevents runtime NullReferenceException and
enables compile-time null safety.
Data Annotations Reference
EF Core uses these attributes to configure the database mapping:
[Table("table_name")] // Maps to specific table
[Key] // Primary key
[Required] // NOT NULL constraint
[MaxLength(200)] // VARCHAR/NVARCHAR length
[Column(TypeName = "...")] // Custom column type
[DatabaseGenerated(...)] // Auto-increment/computedC#
Don't forget to add using System.ComponentModel.DataAnnotations;
and using System.ComponentModel.DataAnnotations.Schema; at the top of your EF Core entity
files.
Reverse: C# to SQL
Polymorpher's converter also works in reverse — paste C# classes and get CREATE TABLE statements
back. This is useful for:
- Generating database migration scripts from code
- Sharing schema definitions with DBA teams
- Creating documentation for your database structure
Try SQL to C# Converter
Paste your CREATE TABLE and get instant EF Core entities, POCOs, or records.
Open Converter →