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

    dotConnect for MySQL 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 MySQL 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.

    Note for Users Who Upgrade dotConnect for MySQL from Earlier Versions

    If you had dotConnect for MySQL v 8.17 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 MySQL 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.MySql.EFCore.NetTopologySuite

    Full .NET Framework

    If you target Full .NET Framework, and you use assemblies, installed by the dotConnect for MySQL installer, you need to add the Devart.Data.MySql.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.UseMySql(
      @""User Id=root;Host=localhost;Database=Test;"",
      x => x.UseNetTopologySuite());
    
    
    optionsBuilder.UseMySql( _
      ""User Id=root;Host=localhost;Database=Test;"", _
      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 geometry type, supported by MySQL: geometry, geometrycollection, linestring, point, polygon, multilinestring, multipoint, multipolygon.

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

    See Also

    Entity Framework Spatials