Avatar ·

C# and SQL Server Data Type Mapping: Complete Guide

Hello everyone! I'm a beginner C# developer writing a small application for working with SQL Server database via Entity Framework. I've encountered a problem: I can't exactly understand which C# data types correspond to which SQL Server types.

\n\n

For example, I have a table with columns:

\n
CREATE TABLE Users (\n    Id INT PRIMARY KEY,\n    Name NVARCHAR(100),\n    Salary DECIMAL(18,2),\n    IsActive BIT,\n    CreatedAt DATETIME2\n);
\n\n

In C# I'm creating a model:

\n
public class User\n{\n    public int Id { get; set; }           // INT -> int, this seems clear\n    public string Name { get; set; }      // NVARCHAR -> string, also clear\n    public decimal Salary { get; set; }   // DECIMAL -> decimal?\n    public bool IsActive { get; set; }    // BIT ->