'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)
Solution 1:[1]
Spatial data isn't supported yet by Entity Framework Core.
Spatial is not planned for 2.0 and we haven’t finished planning for the version after. It might be a good idea to post more details about what you would like to have in spatial support at https://github.com/aspnet/EntityFramework/issues/1100, e.g. do you use specific spatial functions or do you need rich spatial types with all kinds of spatial functionality, would you use this only with a specific database or do you need to write the same code against multiple databases? Etc.
I have made a temporary workaround to include Geography column and use it in EF Core 1.0.0 and above. It's not perfect, but it will do for now. (Similar to Hatef’s answer apparently, wow great minds think alike I guess.)
See the accepted answer on: Entity Framework Core: Type Udt is not supported on this platform. (Spatial Data - Geography)
Solution 2:[2]
It is purely because Entity Framework 7 doesn't support Spatial stuff. Yet. You can follow the github issue related to it here. https://github.com/aspnet/EntityFramework/issues/1100
Solution 3:[3]
EDIT:
Here are the official docs for NetTopologySuite with EF Core (Thanks to ono2012):
Original post:
Update 2018-12-10
Later versions of Entityframework Core now supports spatial data using the NetTopologySuite (see documentation). I think it's supported since 2.1 but I'd recommend 2.2+ due to a bug in 2.1 where coordinates are inverted when saved to database.
Your project needs to target .NET Core 2.1+ (preferably 2.2+).
I managed to get it to work with SQL Server 2016 by adding the following nuget packages:
Microsoft.AspNetCore.All
Microsoft.EntityFrameworkCore.SqlServer.NetTopologySuite
To use it in your DBContext you need to add the following options in your startup.cs:
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<PMSdp3_Context>(o =>
o.UseSqlServer(connString, opt => opt.UseNetTopologySuite()));
}
Solution 4:[4]
With Entity Framework Core 2.2 you can use NetTopologySuite spatial library.
You can then do a query like:
var nearestCity = db.Cities
.OrderBy(c => c.Location.Distance(currentLocation))
.FirstOrDefault();
A complete exemple is available here : https://www.markopapic.com/finding-nearby-users-using-ef-core-spatial-data/
Solution 5:[5]
I used using Microsoft.Spatial in ASP.NET Core project as a replacement for using System.Data.Entity.Spatial and it worked.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Nathan Tuggy |
| Solution 2 | Sai Puli |
| Solution 3 | |
| Solution 4 | Yanga |
| Solution 5 | Adi |
