How to work with spatial types

The LLBLGen Pro designer supports spatial types on SQL Server and PostgreSQL, using both database first and model first. Spatial types are supported on Entity Framework (SQL Server only), Entity Framework Core 2.2 or higher, and LLBLGen Pro Runtime Framework.

Important!

The package Microsoft.SqlServer.Types, used for SQL Server spatial types in our own runtime framework doesn't have an equivalent that's usable on .NET Core. Therefore spatial types for LLBLGen Pro Runtime Framework on SQL Server are only supported on .NET Full.

Internal types

Spatial types in .NET are a bit problematic as there's no central definition of spatial types in the .NET framework which is used by all code needing spatial types: for instance Entity Framework defines the types themselves, as well as SQL Server, which defines a different set of types which represent the same SQL Server spatial types as the ones defined by Entity Framework.

To overcome this, LLBLGen Pro introduces internal types, which are all given a predefined type shortcut. The internal types have an external counterpart which is used at code generation time and which is the .NET type used in your code. These differ per target framework and per target database. To make it easy, tables are given below which illustrate the relationship between the internal types and the external types for the various situations.

The SQL Server types are available in a NuGet package: Microsoft.SqlServer.Types. It's recommended to use the package version which corresponds with the SQL Server version you're using. The NetTopology Suite is an open source library and is also available on NuGet: NetTopologySuite.

Internal Type Type Shortcut
SpatialGeography geography
SpatialGeometry geometry
SpatialGeometryCollection geometry
SpatialLineString linestring
SpatialMultiLineString multilinestring
SpatialMultiPoint multipoint
SpatialMultiPolygon multipolygon
SpatialPoint point
SpatialPolygon polygon

The type shortcut is what you'll work with in field mappings.

Info

If you load a project which was created with LLBLGen Pro v5.5 or earlier and which uses the Microsoft.SqlServer.Types types, they're automatically converted to the internal types SpatialGeometry and SpatialGeography and the used type shortcuts are updated automatically.

LLBLGen Pro Runtime Framework

Internal Type LLBLGen Pro RTF on SQL Server LLBLGen Pro RTF on PostgreSQL
SpatialGeography Microsoft.SqlServer.Types.SqlGeography NetTopologySuite.Geometries.Geometry
SpatialGeometry Microsoft.SqlServer.Types.SqlGeometry NetTopologySuite.Geometries.Geometry
SpatialGeometryCollection Microsoft.SqlServer.Types.SqlGeometry NetTopologySuite.Geometries.GeometryCollection
SpatialLineString Microsoft.SqlServer.Types.SqlGeography NetTopologySuite.Geometries.LineString
SpatialMultiLineString Microsoft.SqlServer.Types.SqlGeography NetTopologySuite.Geometries.MultiLineString
SpatialMultiPoint Microsoft.SqlServer.Types.SqlGeography NetTopologySuite.Geometries.MultiPoint
SpatialMultiPolygon Microsoft.SqlServer.Types.SqlGeography NetTopologySuite.Geometries.MultiPolygon
SpatialPoint Microsoft.SqlServer.Types.SqlGeography NetTopologySuite.Geometries.Point
SpatialPolygon Microsoft.SqlServer.Types.SqlGeography NetTopologySuite.Geometries.Polygon

For our own runtime framework, all spatial types, except SpatialGeometry, will result in SqlGeography at runtime for SQL Server.
Example: an entity field which uses the type shortcut point in a project with LLBLGen Pro Runtime Framework as the target framework and SQL Server as the database, will get SqlGeography as .NET type in the generated code. If the database would have been PostgreSQL, the field's type in the generated code would have been NetTopologySuite.Geometries.Point.

Entity Framework and Entity Framework Core 2.2+

Internal Type Entity Framework on SQL Server Entity Framework Core 2.2+ on SQL Server & PostgreSQL
SpatialGeography Microsoft.SqlServer.Types.SqlGeography NetTopologySuite.Geometries.Geometry
SpatialGeometry Microsoft.SqlServer.Types.SqlGeometry NetTopologySuite.Geometries.Geometry
SpatialGeometryCollection Microsoft.SqlServer.Types.SqlGeometry NetTopologySuite.Geometries.GeometryCollection
SpatialLineString Microsoft.SqlServer.Types.SqlGeography NetTopologySuite.Geometries.LineString
SpatialMultiLineString Microsoft.SqlServer.Types.SqlGeography NetTopologySuite.Geometries.MultiLineString
SpatialMultiPoint Microsoft.SqlServer.Types.SqlGeography NetTopologySuite.Geometries.MultiPoint
SpatialMultiPolygon Microsoft.SqlServer.Types.SqlGeography NetTopologySuite.Geometries.MultiPolygon
SpatialPoint Microsoft.SqlServer.Types.SqlGeography NetTopologySuite.Geometries.Point
SpatialPolygon Microsoft.SqlServer.Types.SqlGeography NetTopologySuite.Geometries.Polygon

For more information regarding spatial types and Entity Framework Core 2.2+, please consult the Entity Framework Core documentation on this topic.

Derived Models

Derived models derive the field types from the entity model field types. This means the types of the derived model element fields have the same internal types as the entity fields they’re mapped on. They therefore inherit the external types for the used target framework on the entity model.