Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
508 views
in Technique[技术] by (71.8m points)

c# - System.Data.Entity.Spatial replacement in ASP.NET Core

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;

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

Is there a way of including this package? Maybe through a NuGet package?

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)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Edit

This feature is new in EF Core 2.2

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


Before EF Core 2.2 versions use this:

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

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. 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");
    }
    
  2. 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);
        }
    
  3. 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.Tehran
2.Ankara
3.Paris
4.Washington DC

Remember! you should select all of records except that field with geography data type!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...