To enable Spatial Type support for PostgreSQL you have to do an extra step when configuring your DBContext:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
services.AddDbContext<SampleContext>(options => | |
{ | |
options.UseNpgsql(configuration.GetConnectionString("PostgreSQL"), o => o.UseNetTopologySuite()); | |
}); |
Calling the UseNetTopologySuite()method activates a plugin for the Npgsql EF Core provider which enables mapping NetTopologySuite's types to PostGIS columns, and even translate many useful spatial operations to SQL. This is the recommended way to interact with spatial types in Npgsql.
To check if the PostGIS extension is installed in your database, you can add the following to your DbContext:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class SampleContextGis:DbContext | |
{ | |
protected override void OnModelCreating(ModelBuilder builder) | |
{ | |
builder.HasPostgresExtension("postgis"); | |
} | |
} |