我的 ASP.NET Core 8 Web API 和 EF Core 8 应用程序无法识别 PostGIS 扩展中的地理类型。这是我得到的错误:
System.InvalidCastException:DataTypeName 为“public.geography”的字段不支持读取为“NetTopologySuite.Geometries.Point”
由于扩展已安装在公共架构中,我尝试更改
search_path
以包含“public”,但它仍然不起作用。
这是已安装的扩展
List of installed extensions
Name | Version | Schema | Description
-------------+---------+------------+------------------------------------------------------------
aws_commons | 1.2 | public | Common data types across AWS services
aws_lambda | 1.0 | public | AWS Lambda integration
plpgsql | 1.0 | pg_catalog | PL/pgSQL procedural language
postgis | 3.4.2 | public | PostGIS geometry and geography spatial types and functions
uuid-ossp | 1.1 | public | generate universally unique identifiers (UUIDs)
这就是数据类型在实体类中的映射方式:
[Column("spatiallocation", TypeName = "geography")]
public Point? SpatialLocation { get; set; }
这就是
DbContext
的注册方式:
services.AddDbContext<GeodataDbContext>(options =>
{
options.UseNpgsql(_configuration.GetConnectionString("GeodataConnectionString"),
b => b
.UseNetTopologySuite()
);
});
仔细检查搜索路径是否设置正确以包含公共架构。您可以在 PostgreSQL 配置中或通过连接字符串设置搜索路径,如下所示:
services.AddDbContext<GeodataDbContext>(options =>
{
options.UseNpgsql("Host=myserver;Database=mydb;Username=myuser;Password=mypassword;Search Path=public",
b => b.UseNetTopologySuite());
});