asp.net-mvc - Entity Framework Core、NpgSql 和 Postgis 异常

标签 asp.net-mvc postgresql entity-framework-core postgis npgsql

我关注了this在我的项目中使用空间数据的文档。

我已经使用 Stack Builder 安装了 postgis 扩展,我创建了模型,迁移,我已经应用它,一切都很好。

问题出在我执行项目时。第一次需要数据库时,我得到了这个异常:

System.InvalidOperationException
  HResult=0x80131509
  Message=The property 'Point.Boundary' is of an interface type ('IGeometry'). If it is a navigation property manually configure the relationship for this property by casting it to a mapped entity type, otherwise ignore the property using the NotMappedAttribute or 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.
  Source=Microsoft.EntityFrameworkCore
  StackTrace:
   at Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.PropertyMappingValidationConvention.Apply(InternalModelBuilder modelBuilder)
   at Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.ConventionDispatcher.ImmediateConventionScope.OnModelBuilt(InternalModelBuilder modelBuilder)
   at Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.CreateModel(DbContext context, IConventionSetBuilder conventionSetBuilder, IModelValidator validator)
   at System.Lazy`1.ViaFactory(LazyThreadSafetyMode mode)
   at System.Lazy`1.ExecutionAndPublication(LazyHelper executionAndPublication, Boolean useDefaultConstructor)
   at System.Lazy`1.CreateValue()
   at Microsoft.EntityFrameworkCore.Internal.DbContextServices.CreateModel()
   at Microsoft.EntityFrameworkCore.Internal.DbContextServices.get_Model()
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite scopedCallSite, ServiceProviderEngineScope scope)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, ServiceProviderEngineScope scope)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite scopedCallSite, ServiceProviderEngineScope scope)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider)
   at Microsoft.EntityFrameworkCore.DbContext.get_DbContextDependencies()
   at Microsoft.EntityFrameworkCore.DbContext.get_InternalServiceProvider()
   at Microsoft.EntityFrameworkCore.Internal.InternalAccessorExtensions.GetService[TService](IInfrastructure`1 accessor)
   at Fleet.Data.Infrastructure.DbContextExtensions.EnsureMigrated(FleetDbContext context) in d:\WorkingCopy\fleet\Fleet.Data\Infrastructure\FleetDbContext.cs:line 99
   at Fleet.Startup.Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) in d:\WorkingCopy\fleet\Fleet\Startup.cs:line 267

继续执行项目的唯一方法是将 NoMapping 属性添加到我的模型的空间数据属性中:

using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using NetTopologySuite.Geometries;

namespace Fleet.Data.Models
{

    public enum GpsSources
    {
        Unknown = 0,
        Shift,
        Refuelling,
        Transport,
        Assistance,
        Workshop
    }


    public class Gps
    {

        [Key]
        public int Id { get; set; }


        [Required]
        public virtual Vehicle Vehicle { get; set; }


        [Required]
        [NotMapped]
        public Point Location { get; set; }


        public string Description { get; set; }

        public GpsSources Source { get; set; }

        public int SourceId { get; set; }

        [Required]
        public virtual ApplicationUser User { get; set; }

        public DateTime Date { get; set; }

    }
}

在 CreateDbContext 方法中,我设置以这种方式使用拓扑套件:

var builder = new DbContextOptionsBuilder<FleetDbContext>();

builder.UseNpgsql(connectionString, o => o.UseNetTopologySuite());

在 OnModelCreating 中,我确保以这种方式存在 postgis 扩展:

builder.HasPostgresExtension("postgis");

在数据库中正确创建了列。

"Location" "public"."geometry" NOT NULL,

最佳答案

对我来说,这在一个项目中有效,但在另一个新项目中无效。

首先,确保实际调用了 DbContextOptionsBuilder。这被设置为在某些情况下使用 sql server,在其他情况下基于 appsetting 使用 postgres。

接下来,这是我最近的错误,我必须对我的数据层进行一些 nuget 包更改:

<PackageReference Include="Microsoft.CodeCoverage" Version="1.0.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="2.2.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.2.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.2.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
<PackageReference Include="NetTopologySuite.IO.GeoJSON" Version="1.15.0" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="2.2.0" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL.Design" 
Version="1.1.1" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL.NetTopologySuite" 
Version="2.2.0" />
<PackageReference Include="Npgsql.GeoJSON" Version="1.0.0" />
<PackageReference Include="Npgsql.NetTopologySuite" Version="4.0.5" />

我不得不从我的单元测试项目中删除“Microsoft.EntityFrameworkCore.InMemory”。

配置数据库:

    var options = new DbContextOptionsBuilder<UserJurisdictionsContext>()
    .UseNpgsql(configuration.GetConnectionString("DbConnection"),
    x => x.UseNetTopologySuite())
        .Options;

    services.AddSingleton(options);

在这些更改之后,我的单元测试成功了。应用程序代码已经在运行。我的代码的每个其他区域看起来都与您添加的相同。

关于asp.net-mvc - Entity Framework Core、NpgSql 和 Postgis 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52329935/

相关文章:

php - SQL查询中的for循环

c# - 使用 EFCore.BulkExtensions 时是否需要调用 SaveChanges

asp.net-mvc - 为什么 bool 复选框的 ASP.NET Core 标记帮助程序不显示标签?

asp.net-mvc - MVC 中的模型是什么。我是否应该将其理解为对其中包含的数据的观点的反射(reflect)?

jquery - 仅验证脏输入

c# - 使用一个属性为 Controller 中的所有操作添加路由前缀

java - 使用 JPA 在 PostgreSQL 中持久化 UUID

sql - 无法计算用于较大 PostgreSQL 查询输出列的 CTE 子查询输出之间的差异

asp.net-core - 自引用多对多关系

c# - 标记为只读的 EF 核心计算属性