Since version 2.2, EF Core supports mapping to spatial data using types from the NetTopologySuite. In this post, I will demonstrate how to use this feature to query entities based on their geolocation.Say we have the following User class:public class User { public string Username { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public Point Location { get; set; } }User's location is represented with the Point class from the NetTopologySuite.Geometries namespace from the NTS. NTS library can be installed via NuGet:Install-Package NetTopologySuite We can instantiate a user the following way:User u = new User { Username = "user1", FirstName = "Maureen", LastName = "Miles", Location = new Point(16.3738, 48.2082) { SRID = 4326 } }; To use the geographical longitude and latitude I did the following:Set the SRID (spatial reference system id) to 4326, which is the spatial reference system used by Google maps (WGS84).Pass the longitude as the first parameter (x) and the latitude as the second parameter (y) to the Point constructor.


I guess you came to this post by searching similar kind of issues in any of the search engine and hope that this resolved your problem. If you find this tips useful, just drop a line below and share the link to others and who knows they might find it useful too.

Stay tuned to my blogtwitter or facebook to read more articles, tutorials, news, tips & tricks on various technology fields. Also Subscribe to our Newsletter with your Email ID to keep you updated on latest posts. We will send newsletter to your registered email address. We will not share your email address to anybody as we respect privacy.


This article is related to


.net,entity-framework,sql-server,db