Entity Framework Core 对 SQL 空间数据类型的支持 - DBGeography? [英] Entity Framework Core support for SQL Spatial Data Types - DBGeography?

查看:23
本文介绍了Entity Framework Core 对 SQL 空间数据类型的支持 - DBGeography?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 ASP.NET Core Razor Pages 制作一个 Web 应用程序,该应用程序在 SQL Server 中有一些地理数据.在使用 EF Core 从现有 SQL 数据库创建 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.

这个链接 是处理这个问题的旧帖子.

This link is an older post dealing with this question.

推荐答案

Entity Framework Core 2.2 版引入了空间数据支持.它使用 NetTopologySuite 数据类型并将它们映射到 geographygeometry 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());
}

然后你可以这样做:

var nearestCity = db.Cities
    .OrderBy(c => c.Location.Distance(currentLocation))
    .FirstOrDefault();

我在我的查找附近用户中写到了它使用实体框架核心空间数据 博客文章.

这篇关于Entity Framework Core 对 SQL 空间数据类型的支持 - DBGeography?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆