ASP.NET Core中的System.Data.Entity.Spatial替换 [英] System.Data.Entity.Spatial replacement in ASP.NET Core

查看:226
本文介绍了ASP.NET Core中的System.Data.Entity.Spatial替换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将Webform从ASP.NET MVC迁移到ASP.NET Core MVC.目前,我正在尝试寻找一种替代方法:

I am trying to migrate a webform from ASP.NET MVC to ASP.NET Core MVC. Currently I am trying to find a way to replace:

using System.Data.Entity.Spatial;

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

since it is not currently available in .NET Core or I may have not been able to find it.

有没有包含此软件包的方法?也许通过NuGet软件包?

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

Ps. I read Microsoft guideline briefly but could not find anything related to it. For anyone who may be in a similar situation, the guide is here: https://docs.asp.net/en/latest/migration/mvc.html

(对不起,如果我不能写一个好问题,我想在这里习惯系统)

(Sorry if I couldn't write a good question, I am trying to get used to the system here)

推荐答案

编辑

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

现在已将空间数据添加到EF Core 2.2中(请参阅文档)

Spatial Data now added to EF Core 2.2 (see documentation)

在EF Core 2.2版本之前使用此:

Before EF Core 2.2 versions use this:

现在您可以将Microsoft.Spatial用于geographygeometry空间操作.

Now you can use Microsoft.Spatial for geography and geometry spatial operations.

ofc,EntityframeworkCore不支持空间,因此您不能在代码中先创建具有地理数据类型的字段,我建议您使用纯SQL命令来做到这一点,直到EntityframeworkCore在2017年第二季度支持空间( ="https://github.com/aspnet/EntityFramework/wiki/Roadmap" rel ="nofollow noreferrer">参见).如果您不知道我该怎么告诉您.

ofc , EntityframeworkCore does not support spatial, so you can not create a field with geography data type in codefirst, I suggest you to do this with pure SQL commends until EntityframeworkCore supports spatial in Q2 2017 (See this). If you don't know how I will tell you.

  1. 首先,您需要添加一个具有地理数据类型的字段, 因此,您需要在向上迁移的类之一中运行此命令:

  1. First of all you need to add a field with geography data type, so you need to run this commend in one of migrations up classes :

protected override void Up(MigrationBuilder migrationBuilder)
{
    migrationBuilder.Sql("ALTER TABLE [dbo].[Cities] ADD [Location] geography");
}

  • 如果您使用的是UnitOfWork,则可以在插入如下记录后更新位置"字段:

  • if you are using UnitOfWork you can update Location field after you inserted a record like this :

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

  • 现在,如果您要查找附近的城市,可以使用此推荐:

  • And now if you want to find nearby cities, you can use this commend :

        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.德黑兰
    2.安卡拉
    3.巴黎
    4.华盛顿特区

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

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

    这篇关于ASP.NET Core中的System.Data.Entity.Spatial替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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