EF Core 3.1 无法映射属性“Geometry.UserData”

Jim*_*Jim 1 c# entity-framework spatial entity-framework-core

我有一个数据库优先的 .net core 3.1 Web 应用程序,它通过一geography列连接到 SQL Server 数据库表。数据库脚手架和应用程序构建完成没有任何问题,但是当我运行应用程序时出现错误。如果我添加该[NotMapped]属性,错误就会消失,但显然该属性未映射。可能是什么问题?

无法映射属性“Geometry.UserData”,因为它属于“object”类型,它不是受支持的基本类型或有效的实体类型。显式映射此属性,或使用 '[NotMapped]' 属性或使用 'OnModelCreating' 中的 'EntityTypeBuilder.Ignore' 忽略它。”

我已经安装了这些软件包

  • Microsoft.EntityFrameworkCore.SqlServer v3.1.8
  • NetTopologySuite.Core v1.15.3

脚手架类看起来像这样

public class MyClass
{  
    public int Id { get; set; }  
    public string Name { get; set; }  
    public NetTopologySuite.Geometries.Geometry Location { get; set; }  
}  
Run Code Online (Sandbox Code Playgroud)

Jim*_*Jim 6

我找到了一个综合了几件事的答案。首先,在安装必要的拓扑包后,重新运行数据库 ef 脚手架。然后,在 Startup.cs 中添加.UseSqlServer(ConnectionString, x=> x.UseNetTopologySuite();. 请记住,生成的 Datacontext 也包含同一行,但由于它是在if语句中写入的,因此不会执行。