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
CodePudding user response:
You are just missing:
UseNetTopologySuite