本页包含有关将空间数据与 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 库解决此问题。
重要
请勿在 macOS 或 Linux 上使用 Microsoft.EntityFrameworkCore.Sqlite 或与 SpatiaLite 配合使用 Microsoft.Data.Sqlite。 默认情况下,这两个包都拉入 SQLitePCLRaw.bundle_e_sqlite3 -- 与系统安装的 SQLite 不兼容的 SQLite 捆绑版本。 使用它可能会导致运行时出现无提示崩溃。 请使用Microsoft.EntityFrameworkCore.Sqlite.Core或Microsoft.Data.Sqlite.Core,并与系统 SQLite 提供程序一起使用,如下所示。
另请注意,系统安装的 SQLite 库的 编译时选项 可能与捆绑版本不同。 这可能会影响数学函数和全文搜索等功能的可用性。 若要快速检查 SQLite 生成中的选项,请运行 PRAGMA compile_options; 并检查应用程序依赖的功能。
将Microsoft.EntityFrameworkCore.Sqlite替换为Microsoft.EntityFrameworkCore.Sqlite.Core并引用SQLitePCLRaw.provider.sqlite3包以使用系统 SQLite 库。
<ItemGroup>
<!-- Use Sqlite.Core with the system SQLite provider instead of Microsoft.EntityFrameworkCore.Sqlite -->
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite.Core" Version="10.0.0" />
<PackageReference Include="SQLitePCLRaw.provider.sqlite3" Version="2.1.10" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite.NetTopologySuite" Version="10.0.0" />
</ItemGroup>
注释
从 EF Core 11(SQLitePCLRaw 3.0)开始,请将 SQLitePCLRaw.provider.sqlite3 版本替换为 3.x.x。 有关详细信息,请参阅 重大变更 。
然后使用 SQLite 之前添加显式初始化:
SQLitePCL.raw.SetProvider(new SQLitePCL.SQLite3Provider_sqlite3());
在 macOS 上,在运行应用之前,还需要设置环境变量,使其使用 Homebrew 的 SQLite 版本:
DYLD_LIBRARY_PATH="$(brew --prefix 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) |
| geometry.AsBinary() | AsBinary(@geometry) |
| 几何.AsText() | AsText(@geometry) |
| 几何。边界 | 边界(@geometry) |
| 几何。缓冲区(距离) | Buffer(@geometry, @distance) |
| 几何.Buffer(distance, quadrantSegments) | Buffer(@geometry, @distance, @quadrantSegments)) |
| 几何。质心 | 质心(@geometry) |
| 几何。Contains(g) | Contains(@geometry,@g) |
| 几何。ConvexHull() | ConvexHull(@geometry) |
| 几何。CoveredBy(g) | CoveredBy(@geometry, @g) |
| 几何体.覆盖(g) | 封面(@geometry, @g) |
| 几何。十字(g) | 十字(@geometry, @g) |
| 几何。差异(其他) | 差异(@geometry, @other) |
| 几何。维 度 | 维度(@geometry) |
| 几何.不相交(g) | 脱节(@geometry, @g) |
| 几何。距离(g) | 距离(@geometry,@g)) |
| 几何。信封 | 信封(@geometry) |
| 几何体.EqualsTopologically(g) | Equals(@geometry,@g)) |
| 几何。GeometryType | GeometryType(@geometry) |
| geometry.GetGeometryN(n) | GeometryN(@geometry,@n + 1) |
| 几何。InteriorPoint | PointOnSurface(@geometry) |
| 几何。交集(其他) | 交集(@geometry, @other) |
| 几何.相交(g) | 相交(@geometry, @g) |
| 几何.IsEmpty | IsEmpty(@geometry) |
| geometry.IsSimple | IsSimple(@geometry) |
| geometry.IsValid | IsValid(@geometry) |
| 几何。IsWithinDistance(geom, distance) | Distance(@geometry, @geom)<= @distance |
| 几何。长度 | GLength(@geometry) |
| 几何。NumGeometries | NumGeometries(@geometry) |
| 几何.NumPoints | NumPoints(@geometry) |
| 几何。OgcGeometryType | CASE GeometryType(@geometry) 当 “POINT” 然后为 1 ...结束 |
| 几何。重叠(g) | 重叠(@geometry, @g) |
| 几何。PointOnSurface | PointOnSurface(@geometry) |
| 几何。Relate(g, intersectionPattern) | 关联(@geometry,@g@intersectionPattern) |
| 几何.Reverse() | ST_Reverse(@geometry) |
| 几何.SRID | SRID(@geometry) |
| geometry.SymmetricDifference(其他) | SymDifference(@geometry,@other)) |
| 几何。ToBinary() | AsBinary(@geometry) |
| 几何.ToText() | AsText(@geometry) |
| 几何.Touches(g) | 触摸(@geometry, @g) |
| 几何。Union() | UnaryUnion(@geometry) |
| 几何。联合(其他) | GUnion(@geometry, @other) |
| 几何体.Within(g) | 在(@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) |
| 点.M | M(@point) |
| 点。X | X(@point) |
| 点Y | Y(@point) |
| 点。Z | Z(@point) |
| 多边形。ExteriorRing | ExteriorRing(@polygon) |
| 多边形.GetInteriorRingN(n) | InteriorRingN(@polygon, @n + 1) |
| polygon.NumInteriorRings | NumInteriorRing(@polygon) |
聚合函数
| .NET | SQL | 在 |
|---|---|---|
| GeometryCombiner.Combine(group.Select(x => x.Property)) | Collect(属性) | EF Core 7.0 |
| ConvexHull.Create(group.Select(x => x.Property)) | ConvexHull(Collect(属性)) | EF Core 7.0 |
| UnaryUnionOp.Union(group.Select(x => x.Property)) | GUnion(属性) | EF Core 7.0 |
| EnvelopeCombiner.CombineAsGeometry(group.Select(x => x.Property)) | Extent(属性) | EF Core 7.0 |