Why PostGIS?
PostGIS extends PostgreSQL with 500+ spatial functions. Combined with NetTopologySuite, you get strongly-typed geometry in .NET.
Setup
builder.Services.AddDbContext<GisDbContext>(o =>
o.UseNpgsql(conn, x => x.UseNetTopologySuite()));
Entity with Geometry
public class Site
{
public int Id { get; set; }
public string Name { get; set; } = string.Empty;
public Point Location { get; set; } = null!; // SRID 4326
}
Distance Query
var userPoint = new Point(72.8777, 19.0760) { SRID = 4326 };
var nearby = await db.Sites
.Where(s => s.Location.Distance(userPoint) < 5000)
.OrderBy(s => s.Location.Distance(userPoint))
.ToListAsync();
💡
Always set SRID on your Point objects. Mixing SRID 4326 (WGS84) and projected coordinates will give wrong distance results.
Conclusion
NetTopologySuite + PostGIS is the most productive GIS stack for .NET developers. No raw SQL, full LINQ support.