This repository has been archived by the owner on Dec 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 635
SQL Server Types
Demis Bellot edited this page Sep 2, 2015
·
7 revisions
OrmLite can be extended to support new Types using SQL Server Special Type Converters which currently adds support for the SQL Server-specific SqlGeography, SqlGeometry and SqlHierarchyId Types.
Since these Types require an external dependency to the Microsoft.SqlServer.Types NuGet package they're contained in a separate NuGet package that can be installed with:
PM> Install-Package ServiceStack.OrmLite.SqlServer.Converters
Alternative Strong-named version:
PM> Install-Package ServiceStack.OrmLite.SqlServer.Converters.Signed
Once installed, all available SQL Server Types can be registered on your SQL Server Provider with:
SqlServerConverters.Configure(SqlServer2012Dialect.Provider);
After the Converters are registered they can treated like a normal .NET Type, e.g:
SqlHierarchyId Example:
public class Node {
[AutoIncrement]
public long Id { get; set; }
public SqlHierarchyId TreeId { get; set; }
}
db.DropAndCreateTable<Node>();
var treeId = SqlHierarchyId.Parse("/1/1/3/"); // 0x5ADE is hex
db.Insert(new Node { TreeId = treeId });
var parent = db.Scalar<SqlHierarchyId>(db.From<Node>().Select("TreeId.GetAncestor(1)"));
parent.ToString().Print(); //= /1/1/
SqlGeography and SqlGeometry Example:
public class GeoTest {
public long Id { get; set; }
public SqlGeography Location { get; set; }
public SqlGeometry Shape { get; set; }
}
db.DropAndCreateTable<GeoTest>();
var geo = SqlGeography.Point(40.6898329,-74.0452177, 4326); // Statue of Liberty
// A simple line from (0,0) to (4,4) Length = SQRT(2 * 4^2)
var wkt = new System.Data.SqlTypes.SqlChars("LINESTRING(0 0, 4 4)".ToCharArray());
var shape = SqlGeometry.STLineFromText(wkt, 0);
db.Insert(new GeoTestTable { Id = 1, Location = geo, Shape = shape });
var dbShape = db.SingleById<GeoTest>(1).Shape;
new { dbShape.STEndPoint().STX, dbShape.STEndPoint().STY }.PrintDump();
Output:
{
STX: 4,
STY: 4
}