我按照这篇文档使用空间数据在我的项目中。
我使用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,