SQL Server EF Core 提供程序中的空间数据

本页包含有关将空间数据与 Microsoft SQL Server 数据库提供程序结合使用的其他信息。 有关在 EF Core 中使用空间数据的一般信息,请参阅主要的空间数据文档。

地理或几何图形

默认情况下,空间属性映射到 SQL Server 中的 geography 列。 若要使用 geometry,请在模型中配置列类型

地理多边形环

当使用 geography 列类型时,SQL Server 对外环(或外壳)和内环(或孔)施加了额外的要求。 外环必须逆时针方向,内环必须顺时针方向。 NetTopologySuite (NTS) 在将值发送到数据库之前对此进行验证。

FullGlobe

当使用 geography 列类型时,SQL Server 具有一个非标准的 geometry 类型来表示整个地球。 它也有一种方法来表示基于整个地球的多边形(没有外环)。 NTS 不支持这两者。

警告

NTS 不支持 FullGlobe 和基于它的多边形。

曲线

如主要的空间数据文档中所述,NTS 当前不能表示曲线。 这意味着,你需要先使用 STCurveToLine 方法转换 CircularString、CompoundCurve 和 CurePolygon 值,然后才能在 EF Core 中使用它们。

警告

NTS 不支持 CircularString、CompoundCurve 和 CurePolygon。

空间函数映射

此表显示了哪些 NTS 成员被转换为哪些 SQL 函数。 请注意,根据列是 geography 类型还是 geometry 类型,转换会有所不同。

.NET SQL (geography) SQL (geometry)
EF.Functions.CurveToLine(geometry) @geometry.STCurveToLine() @geometry.STCurveToLine() EF Core 7.0
geometry.Area @geometry.STArea() @geometry.STArea()
geometry.AsBinary() @geometry.STAsBinary() @geometry.STAsBinary()
geometry.AsText() @geometry.AsTextZM() @geometry.AsTextZM()
geometry.Boundary @geometry.STBoundary()
geometry.Buffer(distance) @geometry.STBuffer(@distance) @geometry.STBuffer(@distance)
geometry.Centroid @geometry.STCentroid()
geometry.Contains(g) @geometry.STContains(@g) @geometry.STContains(@g)
geometry.ConvexHull() @geometry.STConvexHull() @geometry.STConvexHull()
geometry.Crosses(g) @geometry.STCrosses(@g)
geometry.Difference(other) @geometry.STDifference(@other) @geometry.STDifference(@other)
geometry.Dimension @geometry.STDimension() @geometry.STDimension()
geometry.Disjoint(g) @geometry.STDisjoint(@g) @geometry.STDisjoint(@g)
geometry.Distance(g) @geometry.STDistance(@g) @geometry.STDistance(@g)
geometry.Envelope @geometry.STEnvelope()
geometry.EqualsTopologically(g) @geometry.STEquals(@g) @geometry.STEquals(@g)
geometry.GeometryType @geometry.STGeometryType() @geometry.STGeometryType()
geometry.GetGeometryN(n) @geometry.STGeometryN(@n + 1) @geometry.STGeometryN(@n + 1)
geometry.InteriorPoint @geometry.STPointOnSurface()
geometry.Intersection(other) @geometry.STIntersection(@other) @geometry.STIntersection(@other)
geometry.Intersects(g) @geometry.STIntersects(@g) @geometry.STIntersects(@g)
geometry.IsEmpty @geometry.STIsEmpty() @geometry.STIsEmpty()
geometry.IsSimple @geometry.STIsSimple()
geometry.IsValid @geometry.STIsValid() @geometry.STIsValid()
geometry.IsWithinDistance(geom, distance) @geometry.STDistance(@geom)<= @distance @geometry.STDistance(@geom)<= @distance
geometry.Length @geometry.STLength() @geometry.STLength()
geometry.NumGeometries @geometry.STNumGeometries() @geometry.STNumGeometries()
geometry.NumPoints @geometry.STNumPoints() @geometry.STNumPoints()
geometry.OgcGeometryType CASE @geometry.STGeometryType() WHEN N'Point' THEN 1 ...End CASE @geometry.STGeometryType() WHEN N'Point' THEN 1 ...End
geometry.Overlaps(g) @geometry.STOverlaps(@g) @geometry.STOverlaps(@g)
geometry.PointOnSurface @geometry.STPointOnSurface()
geometry.Relate(g, intersectionPattern) @geometry.STRelate(@g,@intersectionPattern)
geometry.SRID @geometry.STSrid @geometry.STSrid
geometry.SymmetricDifference(other) @geometry.STSymDifference(@other) @geometry.STSymDifference(@other)
geometry.ToBinary() @geometry.STAsBinary() @geometry.STAsBinary()
geometry.ToText() @geometry.AsTextZM() @geometry.AsTextZM()
geometry.Touches(g) @geometry.STTouches(@g)
geometry.Union(other) @geometry.STUnion(@other) @geometry.STUnion(@other)
geometry.Within(g) @geometry.STWithin(@g) @geometry.STWithin(@g)
geometryCollection[i] @geometryCollection.STGeometryN(@i + 1) @geometryCollection.STGeometryN(@i + 1)
geometryCollection.Count @geometryCollection.STNumGeometries() @geometryCollection.STNumGeometries()
lineString.Count @lineString.STNumPoints() @lineString.STNumPoints()
lineString.EndPoint @lineString.STEndPoint() @lineString.STEndPoint()
lineString.GetPointN(n) @lineString.STPointN(@n + 1) @lineString.STPointN(@n + 1)
lineString.IsClosed @lineString.STIsClosed() @lineString.STIsClosed()
lineString.IsRing @lineString.IsRing()
lineString.StartPoint @lineString.STStartPoint() @lineString.STStartPoint()
multiLineString.IsClosed @multiLineString.STIsClosed() @multiLineString.STIsClosed()
point.M @point.M @point.M
point.X @point.Long @point.STX
point.Y @point.Lat @point.STY
point.Z @point.Z @point.Z
polygon.ExteriorRing @polygon.RingN(1) @polygon.STExteriorRing()
polygon.GetInteriorRingN(n) @polygon.RingN(@n + 2) @polygon.STInteriorRingN(@n + 1)
polygon.NumInteriorRings @polygon.NumRings() - 1 @polygon.STNumInteriorRing()

聚合函数

.NET SQL
GeometryCombiner.Combine(group.Select(x => x.Property)) CollectionAggregate(Property) EF Core 7.0
ConvexHull.Create(group.Select(x => x.Property)) ConvexHullAggregate(Property) EF Core 7.0
UnaryUnionOp.Union(group.Select(x => x.Property)) UnionAggregate(Property) EF Core 7.0
EnvelopeCombiner.CombineAsGeometry(group.Select(x => x.Property)) EnvelopeAggregate(Property) EF Core 7.0

其他资源