简体   繁体   English

实体框架核心支持SQL空间数据类型 - DBGeography?

[英]Entity Framework Core support for SQL Spatial Data Types - DBGeography?

I want to make a web application using ASP.NET Core Razor Pages that has some geographic data in SQL Server. 我想使用ASP.NET Core Razor Pages制作一个Web应用程序,它在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. 在使用现有SQL DB的EF Core创建EF模型时,Visual Studio会提供错误,指出它不支持Geography数据类型。 Is there a way to use SQL Spatial Data Types like Geography in an ASP.NET Core project using Entity Framework or EF Core? 有没有办法在使用Entity Framework或EF Core的ASP.NET Core项目中使用像Geography这样的SQL空间数据类型?

This link shows some workarounds but nothing out of the box. 链接显示了一些解决方法,但没有开箱即用。

This link is an older post dealing with this question. 此链接是处理此问题的较旧帖子。

Spatial data support is introduced with the version 2.2 of Entity Framework Core. 实体框架核心2.2版引入了空间数据支持。 It uses NetTopologySuite data types and maps them to geography or geometry SQL Server types. 它使用NetTopologySuite数据类型并将它们映射到geographygeometry SQL Server类型。 You can install NetTopologySuite via NuGet: 您可以通过NuGet安装NetTopologySuite:

Install-Package NetTopologySuite

And you will also need the following NuGet package for EF Core spatial data support for SQL Server: 您还需要以下NuGet包,以便为SQL Server提供EF Core空间数据支持:

Install-Package Microsoft.EntityFrameworkCore.SqlServer.NetTopologySuite

and to use the UseNetTopologySuite option in your EF context configuration: 并在EF上下文配置中使用UseNetTopologySuite选项:

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();

I wrote about it in my Finding Nearby Users with Entity Framework Core Spatial Data blog post. 我在我的Finding Nearby Users with Entity Framework Core Spatial Data博客文章中写到了这一点。

I just stumbled across your post and also the first link you mentioned. 我只是偶然发现了你的帖子以及你提到的第一个链接。 Only a few days ago, a preview version of EntityFrameworkCore 2.2.0 was published which is supposed to support spatial data types in SQL Server. 仅在几天前,发布了EntityFrameworkCore 2.2.0的预览版本,该版本应该支持SQL Server中的空间数据类型。

See https://github.com/aspnet/EntityFrameworkCore/issues/1100#issuecomment-417618315 and the following comments. 请参阅https://github.com/aspnet/EntityFrameworkCore/issues/1100#issuecomment-417618315和以下注释。

I haven't tried it myself yet and it's probably not working 100%, but it's surely worth a try. 我自己还没试过,它可能不是100%工作,但它肯定值得一试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM