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

    dotConnect for SQLite 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 dotConnect for SQLite from Earlier Versions

    If you had dotConnect for SQLite v 5.15 or earlier, you need to reset type mapping rules for the provider in Entity Developer when updating to new provider versions in order to have the type new mapping rules for spatial types added.

    To do it, on the Visual Studio Tools menu, point to Entity Developer, and click Options. In the Options dialog box, expand the Entity Developer -> Servers Options node and select SQLite options page. On the selected page, click Reset. The following type mapping rules will be added:

    Server Type

    .NET Type

    geometry NetTopologySuite.Geometries.Geometry
    geometrycollection NetTopologySuite.Geometries.GeometryCollection
    linestring NetTopologySuite.Geometries.LineString
    point NetTopologySuite.Geometries.Point
    polygon NetTopologySuite.Geometries.Polygon
    multilinestring NetTopologySuite.Geometries.MultiLineString
    multipoint NetTopologySuite.Geometries.MultiPoint
    multipolygon NetTopologySuite.Geometries.MultiPolygon

    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.SQLite.EFCore.NetTopologySuite

    Full .NET Framework

    If you target Full .NET Framework, and you use assemblies, installed by the dotConnect for SQLite installer, you need to add the Devart.Data.SQLite.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.

    dotConnect for SQLite uses SpatiaLite SQLite extension for working with spatial data. To get SpatiaLite, you need to install the following NuGet packages:

    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.UseSQLite(
      @""DataSource=mydatabase.db;"",
      x => x.UseNetTopologySuite());
    
    
    optionsBuilder.UseSQLite( _
      ""DataSource=mydatabase.db;"", _
      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 any supported geometry type: geometry, geometrycollection, linestring, point, polygon, multilinestring, multipoint, multipolygon.

    Additionally, it is better to specify SRID (in this example, 4326 is a WGS 84 identifier, that is used for geographical calculations on the Earth surface).:

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