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

EF Core保存NetTopologySuite.Point时触发地理数据类型无效异常

插入NetTopologySuite.Point类型数据时抛出地理数据类型异常

尝试插入包含NetTopologySuite.Point类型属性的数据行时,抛出以下异常:

提供的值不是有效的geography数据类型实例

重现步骤

  1. 创建新的控制台应用。
  2. 添加以下包引用:
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.9" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer.NetTopologySuite" Version="6.0.9" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
  1. 创建如下实体类:
public class SomewhereNice{
    public int Id { get; set; }
    public string Name { get; set; } = "The sea";
    public Point Location { get; set; } = new Point(0, 0, 4326);
}
  1. 创建DbContext派生类:
public class ApplicationDbContext : DbContext{
    public DbSet<SomewhereNice> NicePlaces { get; set; } = null!;
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) {}
}
  1. 修改Program.cs并运行应用:
using EFGeography;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;

Console.WriteLine("Hello, World!");

var services = new ServiceCollection();
services.AddDbContext<ApplicationDbContext>(options =>{
    options.UseSqlServer(
        connectionString: "Server=.\SQLExpress;Database=EFGeography;Trusted_Connection=True;MultipleActiveResultSets=true",
        x => x.UseNetTopologySuite());
});

IServiceProvider serviceProvider = services.BuildServiceProvider();
using var context = serviceProvider.GetRequiredService<ApplicationDbContext>();

context.Database.EnsureCreated();

var theSea = new SomewhereNice();
context.NicePlaces.Add(theSea);
context.SaveChanges();

已验证SRID 4326存在,执行SQL:

select *
from sys.spatial_reference_systems
where authorized_spatial_reference_id = 4326

内容的提问来源于stack exchange,提问作者Peter Morris

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.19 05:55:19