You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

EF Core对接PostgreSQL时Geometry类型不支持问题求助

PostgreSQL + EF Core 空间类型(Geometry)映射问题
  • 现有PostgreSQL数据库,尝试用EF Core绑定,执行上下文scaffolding时收到警告:

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

  • 手动恢复被跳过的Geometry属性:

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; } // 手动添加的属性
}
  • 提取并修改scaffolded生成的实体配置类:
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"); // 手动添加的配置
    }
}
  • 已引用必要库并注册上下文:
builder.Services.AddDbContext<IMyContext, MyContext>(options =>
    options.UseNpgsql(builder.Configuration.GetConnectionString(Constants.ConnectionString)));
  • 操作失败,查询实体或创建初始迁移时均报错:

    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'.

  • 备注:已查阅官方指南及空间类型映射文档,满足所有前提条件但问题仍未解决。

内容的提问来源于stack exchange,提问作者Renat Zamaletdinov

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 02:45:31