【问题标题】:EF Core 3.1 The property 'Geometry.UserData' could not be mappedEF Core 3.1 无法映射属性“Geometry.UserData”
【发布时间】:2022-04-27 01:08:14
【问题描述】:

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

无法映射属性“Geometry.UserData”,因为它属于“对象”类型,不是受支持的原始类型或有效的实体类型。要么显式映射此属性,要么使用 '[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; }  
}  

【问题讨论】:

  • 您正在尝试将Object<T> 添加到数据库中,也许您不应该使用带有Object<T> 的外键作为自己的表?
  • 请在问题中添加表架构。
  • @janzen 该类是使用 ef 工具直接从数据库中构建的。没有人工干预。这就是我问这个问题的原因,因为所有其他表/属性都正确搭建并且可以从数据库中保存/检索。
  • @MortezaAsadi 表模式与 MyClass 完全相同,但 Location 字段的类型设置为 geography
  • read this post,也许有帮助

标签: c# entity-framework entity-framework-core spatial


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    将 NetTopologySuite 5 更新到 6 后,它对我有用

    Program.cs

    builder.Services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection"), sqlOption =>
                sqlOption.UseNetTopologySuite()
             ));
    
    
    
    
     <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.4" />
      <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer.NetTopologySuite" Version="6.0.4" />
      <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.4">
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-01
      • 2021-11-18
      • 2022-01-25
      • 1970-01-01
      • 2020-06-06
      • 1970-01-01
      • 2023-02-10
      • 2021-03-31
      相关资源
      最近更新 更多