c# - ASP.NET Core 中的 System.Data.Entity.Spatial 替换

标签 c# asp.net-core

我正在尝试将 Web 表单从 ASP.NET MVC 迁移到 ASP.NET Core MVC。目前我正在尝试找到一种方法来替换:

using System.Data.Entity.Spatial;

因为它目前在 .NET Core 中不可用,或者我可能找不到它。

有没有办法包含这个包?也许通过 NuGet 包?

附言。我简要阅读了 Microsoft 指南,但找不到与之相关的任何内容。对于任何可能处于类似情况的人,指南在这里: https://docs.asp.net/en/latest/migration/mvc.html

(对不起,如果我不能写出一个好问题,我正在努力适应这里的系统)

最佳答案

编辑

此功能是 EF Core 2.2 中的新增功能

空间数据现已添加到 EF Core 2.2 ( see documentation )


在 EF Core 2.2 版本使用这个之前:

现在您可以使用 Microsoft.Spatial 进行geographygeometry 空间操作。

ofc , EntityframeworkCore 不支持spatial,所以你不能在codefirst中创建一个geography数据类型的字段,我建议你在EntityframeworkCore之前用纯SQL命令来做这个> 在 2017 年第二季度支持空间 (See this)。如果你不知道我会怎么告诉你。

  1. 首先你需要添加一个地理数据类型的字段, 所以你需要在其中一个迁移类中运行这个命令:

    protected override void Up(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.Sql("ALTER TABLE [dbo].[Cities] ADD [Location] geography");
    }
    
  2. 如果您使用的是 UnitOfWork,则可以在插入记录后更新 Location 字段,如下所示:

        try
        {
            City city = new City
            {
                Title = creator.Title
            };
    
            _cities.Add(city);
    
            _uow.ExecuteSqlCommand("UPDATE Cities SET Location = geography::STPointFromText('POINT(' + CAST({0} AS VARCHAR(20)) + ' ' + CAST({1} AS VARCHAR(20)) + ')', 4326) WHERE(ID = {2})", city.Longitude, city.Latitude, city.ID);
    
            return RedirectToAction("Index");
        }
        catch
        {
            return View(creator);
        }
    
  3. 现在如果你想找到附近的城市,你可以使用这个指令:

        var cities = _uow.Set<City>()
            .FromSql(@"DECLARE @g geography = geography::STPointFromText('POINT(' + CAST({0} AS VARCHAR(20)) + ' ' + CAST({1} AS VARCHAR(20)) + ')', 4326);
                       Select ID, Address, CreationDate, CreationDateInPersian, CreationDateStandard, CreatorRealName, CreatorUserID, ExLanguageID, IsActive, IsDeleted, Latitude, Longitude, ModifierRealName, ModifierUserID, ModifyDate, ModifyDateInPersian, ModifyDateStandard, PhoneNumbers, Summary, TimeStamp, Title, Image from Cities
                       ORDER BY Location.STDistance(@g) DESC;",
                       35.738083, 51.591263)
                       .Select(x => new AllRecordsViewModel
                       {
                           ID = x.ID,
                           Title = x.Title
                       })
            .ToList();
    
        return View(cities);
    

// result for nearest cities :

1.Tehran
2.Ankara
3.Paris
4.Washington DC

记住!您应该选择除具有地理数据类型的字段之外的所有记录!

关于c# - ASP.NET Core 中的 System.Data.Entity.Spatial 替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38728227/

相关文章:

c# - "not all code paths return a value"谁能指出我正确的方向?

c# - 为什么 Array.Copy 支持长参数

c# - 有没有办法结合foreach循环

visual-studio-2015 - 无法安装适用于 Windows Server 2012 R2 的 DotNetCore VS2015 Tools Preview2

asp.net-core - asp.net core 2.2 brotli 压缩在 IIS 上不起作用?

c# - Entity Framework Core 排除结果

c# - 在 Razor Pages 中使用 @functions block 的错误和警告

c# - 访问按钮父级

c# - 使用参数更正工厂方法的实现

asp.net-mvc - 防伪验证异常 : The provided anti forgery token was meant for a different claims-based user than the current user