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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…