Spatial data is the data about the location of places. In our case, we are interested in the location of places on our planet earth. When we talk about spatial queries, we mean queries we can make to databases that contain spatial data.
Some examples of a space queries would be: What are the restaurants closest to my current location? What tourist attractions are located within 3 miles from my current location?
Starting with Entity Framework Core 2.2, and with the help of the NetTopologySuite library, we can make space queries.
In this post we will see how to use EF Core 3.1 to perform spatial queries to a SQL Server database to determine which restaurants are closest to our current location.
This is a step by step video tutorial on the subject:
Now a summary of the video:
Github Repo: https://github.com/gavilanch/EFCoreSpatialQueries
We install the following libraries:
Microsoft.EntityFrameworkCore.Tools (optional: only needed if you are going to use Visual Studio on Windows)
Microsoft.Extensions.Logging.Console (optional: I use this one to display the queries generated by EF Core in the console)
We create the Restaurant model, and in it we use the Point data type, which comes from NetTopologySuite.Geometries, this to be able to save the geographic location of a restaurant:
Then, we create a class that inherits from DbContext, and we configure it to use SQL Server. In addition, we configure the NetTopologySuite to work with EF Core. Below, we also set up test data for our Restaurant entity:
Note that in the OnModelCreating we use the geometryFactory with srid equal to 4326 (WGS 84). This is the standard in cartography and GPS systems. Therefore, for locations on our planet, it is the most used.
Then, we can add the migration and send the changes to the database.
Finally, we can make queries to our Restaurants table. For example, let’s say we want to query all the restaurants whose distance to us is less than 2 km (2,000 meters). For this, we can say:
Here we are hardcoding the user’s location, however, in real life, this value will come from the user’s device.