gpt4 book ai didi

c# - 在 Entity Framework 中使用 Postgis 的几何类型

转载 作者:行者123 更新时间:2023-12-04 02:31:52 24 4
gpt4 key购买 nike

我一直在尝试使用 ASP.NET Core 构建我的第一个 API 项目。

除了一件事,一切都很顺利。每当我尝试添加一个新的脚手架 Controller (当然是在添加我的模型之后)时,我都会收到以下错误:

The property 'Favor.Coordinates' is of type 'Geometry' which is not supported by current database provider. Either change the property CLR type or ignore the property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.

这是不言自明的;我有一个带有 Geometry 类型的模型类(来自 Postgis),这似乎导致了 Entity Framework 的问题。

我做了什么?

  • 我已经创建了我的数据库并添加了 Postgis 扩展以便能够使用几何数据类型
  • 我在我的项目中添加了Npgsql.EntityFrameworkCore.PostgreSQL.NetTopologySuite包来支持Geometry,详见以下教程:https://learn.microsoft.com/en-us/ef/core/modeling/spatial
  • 我已经添加了所有必需的 Npgsql 扩展以将 PostgreSQL 与 ASP.NET 一起使用,并从数据库生成了所有模型类

值得一提的是,正如教程中提到的,我在生成模型之前还添加了 NetTopologySuite 包。然而,每当我尝试使用 Entity Framework 添加脚手架 API Controller 时,我都会收到上述错误,尽管事实上 NTS 支持 Geometry

为了进一步引用,这里是错误提到的 Favor 类:

using System;
using System.Collections.Generic;
using NetTopologySuite.Geometries;

namespace FavorAPI.Models
{
public partial class Favor
{
public Favor()
{
Payment = new HashSet<Payment>();
Request = new HashSet<Request>();
}

public int Idfavor { get; set; }
public string Description { get; set; }
public string Title { get; set; }
public DateTime? Creationtime { get; set; }
public DateTime? Happeningtime { get; set; }
public double? Price { get; set; }
public short? Estimatedtime { get; set; }
public short? Maxpeople { get; set; }
public short? Currentpeople { get; set; }
public Geometry Coordinates { get; set; } //<-- Problematic!
public int? Idstatus { get; set; }
public int? Idposter { get; set; }
public int? Idcategory { get; set; }

public virtual Category IdcategoryNavigation { get; set; }
public virtual Individual IdposterNavigation { get; set; }
public virtual Favorstatus IdstatusNavigation { get; set; }
public virtual ICollection<Payment> Payment { get; set; }
public virtual ICollection<Request> Request { get; set; }
}
}

我还在我的数据库上下文中添加了 x => x.UseNetTopologySuite()。有谁知道我可能做错了什么?我总是可以添加错误中提到的 [NotMapped] 属性,但我确实希望映射此属性。

提前致谢!

编辑:手动创建 Controller (而不是通过脚手架)可以工作并且不会抛出任何错误,但如果我尝试访问到任何 Controller 端点的路由,最终会出现相同的错误消息。

最佳答案

已解决。事实证明,我不应该只在 DB 上下文文件中包含 x => x.UseNetTopologySuite(),但也在 Startup.cs 中。呸!

services.AddDbContext<MyDBContext>(options =>
options.UseNpgsql(Configuration.GetConnectionString("MyDatabaseString"), x => x.UseNetTopologySuite()) );

关于c# - 在 Entity Framework 中使用 Postgis 的几何类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63770589/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com