实体框架对SQL空间数据类型的核心支持-DBGeography? [英] Entity Framework Core support for SQL Spatial Data Types - DBGeography?
问题描述
我想使用ASP.NET Core Razor Pages创建一个Web应用程序,该页面在SQL Server中具有一些地理数据。当从现有SQL数据库中使用EF Core创建EF模型时,Visual Studio会给出一个错误,提示它不支持Geography数据类型。是否可以使用Entity Framework或EF Core在ASP.NET Core项目中使用SQL空间数据类型(如地理)?
I want to make a web application using ASP.NET Core Razor Pages that has some geographic data in SQL Server. Visual Studio gives an error that it doesn't support the Geography data type when creating the EF model using EF Core from an existing SQL DB. Is there a way to use SQL Spatial Data Types like Geography in an ASP.NET Core project using Entity Framework or EF Core?
此链接显示了一些解决方法,但没有现成的方法。
This link shows some workarounds but nothing out of the box.
< a href = https://stackoverflow.com/questions/38908803/how-to-use-sqlserver-types-spatial-types-within-asp-net-core-1-0-application>此链接是处理此问题的旧帖子。
This link is an older post dealing with this question.
推荐答案
空间数据支持是在Entity Framework Core的2.2版中引入的。它使用 NetTopologySuite 数据类型并将其映射到 geography
或 geometry
SQL Server类型。
您可以通过NuGet安装NetTopologySuite:
Spatial data support is introduced with the version 2.2 of Entity Framework Core. It uses NetTopologySuite data types and maps them to geography
or geometry
SQL Server types.
You can install NetTopologySuite via NuGet:
Install-Package NetTopologySuite
您还将需要以下NuGet包,以支持SQL Server的EF Core空间数据:
And you will also need the following NuGet package for EF Core spatial data support for SQL Server:
Install-Package Microsoft.EntityFrameworkCore.SqlServer.NetTopologySuite
并在EF上下文配置中使用 UseNetTopologySuite
选项:
and to use the UseNetTopologySuite
option in your EF context configuration:
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(
@"my connection string",
x => x.UseNetTopologySuite());
}
然后您可以执行以下操作:
Then you can do something like this:
var nearestCity = db.Cities
.OrderBy(c => c.Location.Distance(currentLocation))
.FirstOrDefault();
我在使用Entity Framework Core Spatial Data查找附近的用户博客文章。
这篇关于实体框架对SQL空间数据类型的核心支持-DBGeography?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!