Migrating from a legacy database to Entity Framework Core? Converting SQL DDL scripts into C# entity classes by hand is tedious, error-prone, and wastes hours of developer time. In this guide, you'll learn how to convert SQL CREATE TABLE statements to EF Core entities — automatically, with proper type mapping and data annotations.
The Problem: Manual SQL-to-C# Translation
Every database migration project starts the same way: you have dozens (or hundreds) of
CREATE TABLE statements and need corresponding C# classes. Each column needs correct type
mapping, nullable annotations, string length constraints, and primary key markers. Getting even one wrong
causes runtime failures.
Common mapping mistakes include:
- Mapping
DECIMAL(18,2)todoubleinstead ofdecimal(precision loss) - Forgetting
[Required]forNOT NULLNVARCHARcolumns - Missing
[MaxLength]attributes for bounded string columns - Incorrect
UNIQUEIDENTIFIER→Guidmapping
SQL to C# Type Mapping Reference
Here's the complete SQL Server → C# type mapping used by Polymorpher's converter:
SQL Type → C# Type
─────────────────────────────────────
INT → int
BIGINT → long
SMALLINT → short
TINYINT → byte
BIT → bool
DECIMAL(p,s) → decimal
FLOAT → double
REAL → float
MONEY → decimal
VARCHAR(n) / TEXT → string
NVARCHAR(n) / NTEXT → string
CHAR(n) / NCHAR(n) → string
DATETIME / DATETIME2 → DateTime
DATE → DateOnly (.NET 6+)
TIME → TimeOnly (.NET 6+)
DATETIMEOFFSET → DateTimeOffset
UNIQUEIDENTIFIER → Guid
VARBINARY / IMAGE → byte[]
XML → stringType Mapping
Example: Full Conversion
Input: SQL CREATE TABLE
CREATE TABLE Products (
ProductId INT NOT NULL IDENTITY(1,1) PRIMARY KEY,
Name NVARCHAR(200) NOT NULL,
Description NVARCHAR(MAX) NULL,
Price DECIMAL(18,2) NOT NULL,
StockCount INT NOT NULL DEFAULT 0,
CategoryId INT NOT NULL,
IsActive BIT NOT NULL DEFAULT 1,
CreatedAt DATETIME2 NOT NULL DEFAULT GETUTCDATE(),
UpdatedAt DATETIME2 NULL
);SQL
Output: EF Core Entity with Data Annotations
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
[Table("Products")]
public class Product
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ProductId { get; set; }
[Required]
[MaxLength(200)]
public string Name { get; set; } = string.Empty;
public string? Description { get; set; }
[Required]
[Column(TypeName = "decimal(18,2)")]
public decimal Price { get; set; }
public int StockCount { get; set; }
[Required]
public int CategoryId { get; set; }
public bool IsActive { get; set; } = true;
[Required]
public DateTime CreatedAt { get; set; }
public DateTime? UpdatedAt { get; set; }
}C#
Polymorpher's converter supports three output modes: EF Core Entity (with full data annotations), POCO (plain class with auto-properties), and C# Record (immutable positional record). Choose based on your project architecture.
POCO vs. Record Output
POCO Class (Plain Old CLR Object)
public class Product
{
public int ProductId { get; set; }
public string Name { get; set; } = string.Empty;
public string? Description { get; set; }
public decimal Price { get; set; }
public int StockCount { get; set; }
public int CategoryId { get; set; }
public bool IsActive { get; set; }
public DateTime CreatedAt { get; set; }
public DateTime? UpdatedAt { get; set; }
}C#
C# Record (Immutable)
public record Product(
int ProductId,
string Name,
string? Description,
decimal Price,
int StockCount,
int CategoryId,
bool IsActive,
DateTime CreatedAt,
DateTime? UpdatedAt
);C#
Configuring EF Core DbContext
After generating your entity classes, register them in your DbContext:
public class AppDbContext : DbContext
{
public DbSet<Product> Products => Set<Product>();
protected override void OnModelCreating(ModelBuilder builder)
{
builder.Entity<Product>(entity =>
{
entity.HasIndex(e => e.CategoryId);
entity.Property(e => e.CreatedAt)
.HasDefaultValueSql("GETUTCDATE()");
});
}
}C#
Try SQL to C# Converter
Paste your CREATE TABLE SQL and get instant EF Core entities. Free, no sign-up.
Open Converter →Conclusion
Converting SQL to EF Core entities doesn't have to be manual drudgery. Use Polymorpher's SQL to Classes converter to generate properly annotated C# entities in seconds. Whether you need EF Core entities with full data annotations, clean POCOs, or immutable records — the converter handles the type mapping, naming conventions, and nullable annotations for you.
For JSON-based data, check out our JSON to Classes converter, and use the Code Beautifier to format your SQL queries before converting.