Renat Zamaletdinov
Renat Zamaletdinov

Reputation: 1232

The property X is of type 'Geometry' which is not supported by the current database provider

I was provided with a PostgreSQL database and currently trying to bind EF Core to it. As the DB already exists I tried to scaffold a context, and I succeeded with warnings similar to:

Could not find type mapping for column 'x.Polygons.Shape' with data type 'x.geometry'. Skipping column.

I skipped all such columns at scaffolding time and tried restoring them manually:

public sealed class Polygon
{
    public int Id { get; set; }
    public DateOnly CreationDate { get; set; }
    public DateOnly ModificationDate { get; set; }
    public DateOnly SurveyDate { get; set; }
    public Geometry Shape { get; set; } //This (and similar) column was added manually
}

After that I extracted and patched the scaffolded entity configuration to a separate class like this:

public sealed class PolygonConfiguration : IEntityTypeConfiguration<Polygon>
{
    public void Configure(EntityTypeBuilder<Polygon> builder)
    {
        builder.ToTable("Polygons", Constants.DatabaseScheme);
        builder.HasIndex(e => e.Id, "idx_Polygons_ID");
        builder.Property(e => e.Id).HasColumnName("ID");
        builder.Property(x => x.Shape).HasColumnType("geometry"); //This one was added manually as well
    }
}

Referenced all required libraries and registered my context:

builder.Services.AddDbContext<IMyContext, MyContext>(options =>
    options.UseNpgsql(builder.Configuration.GetConnectionString(Constants.ConnectionString)));

But it does not work. I tried to query entities with Geometry type. I also tried to create the initial EF migration (to recreate the DB) but ended up with absolutely the same error:

The property 'Polygon.Shape' is of type 'Geometry' which is not supported by the current database provider. Either change the property CLR type, or ignore the property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.

P.S. I have already checked some guides here and here - they didn't help as I have met all the prerequisites

Upvotes: 1

Views: 1377

Answers (1)

ErikEJ
ErikEJ

Reputation: 41799

You are just missing:

UseNetTopologySuite

EF Core supports mapping to spatial data types using the NetTopologySuite spatial library.

EF Core Provider Spatial NuGet Package
Microsoft.EntityFrameworkCore.SqlServer Microsoft.EntityFrameworkCore.SqlServer.NetTopologySuite
Microsoft.EntityFrameworkCore.Sqlite Microsoft.EntityFrameworkCore.Sqlite.NetTopologySuite
Microsoft.EntityFrameworkCore.InMemory NetTopologySuite
Npgsql.EntityFrameworkCore.PostgreSQL Npgsql.EntityFrameworkCore.PostgreSQL.NetTopologySuite
Pomelo.EntityFrameworkCore.MySql Pomelo.EntityFrameworkCore.MySql.NetTopologySuite
Devart.Data.MySql.EFCore Devart.Data.MySql.EFCore.NetTopologySuite
Devart.Data.PostgreSql.EFCore Devart.Data.PostgreSql.EFCore.NetTopologySuite
Devart.Data.SQLite.EFCore Devart.Data.SQLite.EFCore.NetTopologySuite
Teradata.EntityFrameworkCore Teradata.EntityFrameworkCore.NetTopologySuite

Upvotes: 5

Related Questions