Skip to content

Instantly share code, notes, and snippets.

@PiotrFerenc
Created June 17, 2025 05:10
Show Gist options
  • Save PiotrFerenc/962b122a4a4447dd3e537513b5d4fe98 to your computer and use it in GitHub Desktop.
Save PiotrFerenc/962b122a4a4447dd3e537513b5d4fe98 to your computer and use it in GitHub Desktop.
public static string MapSqlServerTypeToFluentMigrator(string sqlType)
{
sqlType = sqlType.ToLowerInvariant();
if (sqlType.StartsWith("varchar") || sqlType.StartsWith("nvarchar") || sqlType.StartsWith("char") || sqlType.StartsWith("nchar") || sqlType.StartsWith("text") || sqlType.StartsWith("ntext"))
return "AsString()";
if (sqlType.StartsWith("int"))
return "AsInt32()";
if (sqlType.StartsWith("bigint"))
return "AsInt64()";
if (sqlType.StartsWith("smallint"))
return "AsInt16()";
if (sqlType.StartsWith("tinyint"))
return "AsByte()";
if (sqlType.StartsWith("bit"))
return "AsBoolean()";
if (sqlType.StartsWith("decimal") || sqlType.StartsWith("numeric") || sqlType.StartsWith("money") || sqlType.StartsWith("smallmoney"))
return "AsDecimal()";
if (sqlType.StartsWith("float"))
return "AsDouble()";
if (sqlType.StartsWith("real"))
return "AsSingle()";
if (sqlType.StartsWith("datetime") || sqlType.StartsWith("smalldatetime") || sqlType.StartsWith("datetime2"))
return "AsDateTime()";
if (sqlType.StartsWith("date"))
return "AsDate()";
if (sqlType.StartsWith("time"))
return "AsTime()";
if (sqlType.StartsWith("uniqueidentifier"))
return "AsGuid()";
if (sqlType.StartsWith("binary") || sqlType.StartsWith("varbinary") || sqlType.StartsWith("image") || sqlType.StartsWith("rowversion") || sqlType.StartsWith("timestamp"))
return "AsBinary()";
return "AsCustom(\"" + sqlType + "\")"; // fallback
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment