dotConnect for PostgreSQL Documentation
In This Topic
    Spatial Data Support for EF Core
    In This Topic

    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.

    Note for Users Who Upgrade to EF Core from EF5 or EF6

    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.

    Linking NetTopologySuite to Provider

    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.

    NetTopologySuite Configuration

    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:

    optionsBuilder.UsePostgreSql(
      @""host=server;database=test;user id=postgres;"",
      x => x.UseNetTopologySuite());
    
    
    optionsBuilder.UsePostgreSql( _
      ""host=server;database=test;user id=postgres;"", _
      Function(x) x.UseNetTopologySuite())
    
    

    Supported NetTopologySuite Data Types

    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.

    Mapping NetTopologySuite Types to Database Data Types

    Suppose, we have the following class:

       public class City {
        public int Id { get; set; }
        public Point Geometry { get; set; }
        [MaxLength(200)]
        public string Name { get; set; }
      }
    
    
    Public Class City
        Public Property Id As Integer
        Public Property Geometry As Point
        <MaxLength(200)>
        Public Property Name As String
    End Class
    
    

    You can specify geometry or geography type:

       modelBuilder.Entity<City>()
        .Property(p => p.Geometry)
        .HasColumnType("geography");
    
    
    modelBuilder.Entity(Of City)().[Property](Function(p) p.Geometry).HasColumnType("geography")
    
    

    See Also

    Entity Framework Spatials