dotConnect for PostgreSQL supports working with Spatial data in Entity Framework Core 3 and 5 via the NetTopologySuite GIS library.
The spatial service used is specified in the SpatialServiceType property.
Entity Framework v5 and v6 support spatial data types. These types are represented as two DbGeometry and DbGeography data types from System.Data.Entity.dll in .NET Framework 4.5 (Entity Framework 5) or from EntityFramework.dll (Entity Framework 6). However, these classes themselves were not independent, but they were rather wrappers for full-featured classes from some third-party GIS library.
dotConnect for PostgreSQL supports spatial data for Entity Framework v5 and v6 via the NetTopologySuite 1.x library. Upgrade process would be complicated not just because you need to use the corresponding NetTopologySuite classes (Geometry, Point, LineString, Polygon, etc.), but also because of partial incompatibility between EF Core and EF v5 and v6.
If you create an Entity Framework Core model in Entity Developer via the database-first approach, it downloads all the necessary NuGet packages and links assemblies automatically. If you use code-first approach, and write the classes and mapping code yourself, you will need to perform additional actions.
.NET Core 2 or Higher
If you target .NET Core 2, .NET Core 3 or .NET 5, and you use dotConnect NuGet packages, all the necessary assemblies are loaded automatically. Just install the corresponding NuGet package - Execute the following command in the Package Manager Console:
Install-Package Devart.Data.PostgreSql.EFCore.NetTopologySuite
Full .NET Framework
If you target Full .NET Framework, and you use assemblies, installed by the dotConnect for PostgreSQL installer, you need to add the Devart.Data.PostgreSql.Entity.EFCore.NetTopologySuite.dll assembly from the Entity/EFCore3 subfolder of the provider installation folder to the project references.
Additionally, you need to install the NetTopologySuite NuGet package of version 2.1.0.
Call the UseNetTopologySuite() method for DbContext options builder of the corresponding provider to link NetTopologySuite to the provider and enable the ability to map properties to spatial data types. Here is the example:
Our Entity Framework Core provider supports a number of NetTopologySuite data types. Geometry is the base type for them, and you can use it in your application. However, you may use specific data types for properties if the corresponding database column stores only corresponding spatial figures:
Class |
Brief Description |
---|---|
Geometry | Abstract base class for all spatial data types. |
GeometryCollection | A collection of geometry objects. |
LineString | A sequence of two or more vertices with all points along the linearly-interpolated curves (line segments) between each pair of consecutive vertices. |
Point | A single point. |
Polygon | A polygon with linear edges. |
MultiLineString | A collection of LineStrings. |
MultiPoint | A collection of Points. |
MultiPolygon | A collection of Polygons. |
Suppose, we have the following class:
You can specify geometry or geography type: