繁体   English   中英

Entity Framework Core 保存 geography::Point 抛出“提供的值不是数据类型 geography 的有效实例”

[英]Entity Framework Core saving geography::Point throws "The supplied value is not a valid instance of data type geography"

尝试插入具有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" />

(3)创建如下域class

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);
}

(4)添加一个DbContext后代

public class ApplicationDbContext : DbContext
{
    public DbSet<SomewhereNice> NicePlaces { get; set; } = null!;
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) {}
}

(5)更新Program.cs文件如下,然后运行app....

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确实存在。

select *
from sys.spatial_reference_systems
where authorized_spatial_reference_id = 4326

new Point(0, 0, 4326)创建一个点 X,Y,Z

我需要的是

new Point(0, 0) { SRID = 4326 };

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM