Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework Core support for SQL Spatial Data Types - DBGeography?

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.

like image 202
Heinrich Avatar asked Aug 04 '18 02:08

Heinrich


People also ask

How to enable spatial data support in Entity Framework Core?

Spatial data support is introduced with the version 2.2 of Entity Framework Core. It uses NetTopologySuitedata types and maps them to geographyor geometrySQL Server types. You can install NetTopologySuite via NuGet: Install-Package NetTopologySuite And you will also need the following NuGet package for EF Core spatial data support for SQL Server:

Does Visual Studio support geography data type in Entity Framework Core?

c#entity-framework-core Question 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.

Can I use SQL spatial data types like geography in Visual Studio?

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? Thislink shows some workarounds but nothing out of the box.

Which NuGet package for EF Core spatial data support for SQL Server?

And you will also need the following NuGet package for EF Core spatial data support for SQL Server: Install-Package Microsoft.EntityFrameworkCore.SqlServer.NetTopologySuite


1 Answers

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

And you will also need the following NuGet package for EF Core spatial data support for SQL Server:

Install-Package Microsoft.EntityFrameworkCore.SqlServer.NetTopologySuite

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

Then you can do something like this:

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

I wrote about it in my Finding Nearby Users with Entity Framework Core Spatial Data blog post.

like image 192
Marko Papic Avatar answered Sep 18 '22 18:09

Marko Papic