此页面包含有关将空间数据与 SQLite 数据库提供程序结合使用的其他信息。 有关在 EF Core 中使用空间数据的一般信息,请参阅主要的空间数据文档。
安装 SpatiaLite
在 Windows 上,本机 mod_spatialite 库作为 NuGet 包依赖项分发。 其他平台需要单独安装。 这通常是使用软件包管理器完成的。 例如,可以在 Debian 和 Ubuntu 上使用 APT 以及在 MacOS 上使用 Homebrew。
# Debian/Ubuntu
apt-get install libsqlite3-mod-spatialite
# macOS
brew install libspatialite
遗憾的是,较新版本的 PROJ(SpatiaLite 的依赖项)与 EF 的默认 SQLitePCLRaw 捆绑包不兼容。 改为使用系统 SQLite 库便可解决此问题。
<ItemGroup>
  <!-- Use bundle_sqlite3 instead with SpatiaLite on macOS and Linux -->
  <!--<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="3.1.0" />-->
  <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite.Core" Version="3.1.0" />
  <PackageReference Include="SQLitePCLRaw.bundle_sqlite3" Version="2.0.4" />
  <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite.NetTopologySuite" Version="3.1.0" />
</ItemGroup>
在 macOS 上,还需在运行应用之前设置环境变量,以便它使用 Homebrew 的 SQLite 版本。 在 Visual Studio for Mac 中,可以在“项目”>“项目选项”>“运行”>“配置”>“默认值”下进行设置
DYLD_LIBRARY_PATH=/usr/local/opt/sqlite/lib
配置 SRID
在 SpatiaLite 中,列需要为每个列指定一个 SRID。 默认 SRID 为 0。 使用 HasSrid 方法指定其他 SRID。
modelBuilder.Entity<City>().Property(c => c.Location)
    .HasSrid(4326);
注意
4326 指的是 WGS 84,一种在 GPS 和其他地理系统中使用的标准。
维度
列的默认维度(或坐标)为 X 和 Y。要启用 Z 或 M 等其他坐标,请配置列类型。
modelBuilder.Entity<City>().Property(c => c.Location)
    .HasColumnType("POINTZ");
空间函数映射
此表显示哪些了 NetTopologySuite (NTS) 成员被转换为哪些 SQL 函数。
| .NET | SQL | 
|---|---|
| geometry.Area | Area(@geometry) | 
| geometry.AsBinary() | AsBinary(@geometry) | 
| geometry.AsText() | AsText(@geometry) | 
| geometry.Boundary | Boundary(@geometry) | 
| geometry.Buffer(distance) | Buffer(@geometry, @distance) | 
| geometry.Buffer(distance, quadrantSegments) | Buffer(@geometry, @distance, @quadrantSegments) | 
| geometry.Centroid | Centroid(@geometry) | 
| geometry.Contains(g) | Contains(@geometry, @g) | 
| geometry.ConvexHull() | ConvexHull(@geometry) | 
| geometry.CoveredBy(g) | CoveredBy(@geometry, @g) | 
| geometry.Covers(g) | Covers(@geometry, @g) | 
| geometry.Crosses(g) | Crosses(@geometry, @g) | 
| geometry.Difference(other) | Difference(@geometry, @other) | 
| geometry.Dimension | Dimension(@geometry) | 
| geometry.Disjoint(g) | Disjoint(@geometry, @g) | 
| geometry.Distance(g) | Distance(@geometry, @g) | 
| geometry.Envelope | Envelope(@geometry) | 
| geometry.EqualsTopologically(g) | Equals(@geometry, @g) | 
| geometry.GeometryType | GeometryType(@geometry) | 
| geometry.GetGeometryN(n) | GeometryN(@geometry, @n + 1) | 
| geometry.InteriorPoint | PointOnSurface(@geometry) | 
| geometry.Intersection(other) | Intersection(@geometry, @other) | 
| geometry.Intersects(g) | Intersects(@geometry, @g) | 
| geometry.IsEmpty | IsEmpty(@geometry) | 
| geometry.IsSimple | IsSimple(@geometry) | 
| geometry.IsValid | IsValid(@geometry) | 
| geometry.IsWithinDistance(geom, distance) | Distance(@geometry, @geom)<= @distance | 
| geometry.Length | GLength(@geometry) | 
| geometry.NumGeometries | NumGeometries(@geometry) | 
| geometry.NumPoints | NumPoints(@geometry) | 
| geometry.OgcGeometryType | CASE GeometryType(@geometry) WHEN 'POINT' THEN 1 ... END | 
| geometry.Overlaps(g) | Overlaps(@geometry, @g) | 
| geometry.PointOnSurface | PointOnSurface(@geometry) | 
| geometry.Relate(g, intersectionPattern) | Relate(@geometry, @g, @intersectionPattern) | 
| geometry.Reverse() | ST_Reverse(@geometry) | 
| geometry.SRID | SRID(@geometry) | 
| geometry.SymmetricDifference(other) | SymDifference(@geometry, @other) | 
| geometry.ToBinary() | AsBinary(@geometry) | 
| geometry.ToText() | AsText(@geometry) | 
| geometry.Touches(g) | Touches(@geometry, @g) | 
| geometry.Union() | UnaryUnion(@geometry) | 
| geometry.Union(other) | GUnion(@geometry, @other) | 
| geometry.Within(g) | Within(@geometry, @g) | 
| geometryCollection[i] | GeometryN(@geometryCollection, @i + 1) | 
| geometryCollection.Count | NumGeometries(@geometryCollection) | 
| lineString.Count | NumPoints(@lineString) | 
| lineString.EndPoint | EndPoint(@lineString) | 
| lineString.GetPointN(n) | PointN(@lineString, @n + 1) | 
| lineString.IsClosed | IsClosed(@lineString) | 
| lineString.IsRing | IsRing(@lineString) | 
| lineString.StartPoint | StartPoint(@lineString) | 
| multiLineString.IsClosed | IsClosed(@multiLineString) | 
| point.M | M(@point) | 
| point.X | X(@point) | 
| point.Y | Y(@point) | 
| point.Z | Z(@point) | 
| polygon.ExteriorRing | ExteriorRing(@polygon) | 
| polygon.GetInteriorRingN(n) | InteriorRingN(@polygon, @n + 1) | 
| polygon.NumInteriorRings | NumInteriorRing(@polygon) | 
聚合函数
| .NET | SQL | 在 | 
|---|---|---|
| GeometryCombiner.Combine(group.Select(x => x.Property)) | Collect(Property) | EF Core 7.0 | 
| ConvexHull.Create(group.Select(x => x.Property)) | ConvexHull(Collect(Property)) | EF Core 7.0 | 
| UnaryUnionOp.Union(group.Select(x => x.Property)) | GUnion(Property) | EF Core 7.0 | 
| EnvelopeCombiner.CombineAsGeometry(group.Select(x => x.Property)) | Extent(Property) | EF Core 7.0 |