Why am I getting this error when trying to implement UseNetTopologySuite in my ASP.Net Core 2.2 api?

Sco*_*tie 5 nettopologysuite ef-core-2.0

I am attempting to use the geometry functionality in Sql Server and EF Core.

I've installed the following packages

<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer.NetTopologySuite" Version="2.2.6" />
<PackageReference Include="NetTopologySuite" Version="2.0.0" />
Run Code Online (Sandbox Code Playgroud)

In my Startup.cs, I have the following:

services.AddDbContextPool<CRFlowContext>(options => 
    options.UseSqlServer("connection string", x => x.UseNetTopologySuite());
});
Run Code Online (Sandbox Code Playgroud)

Everything builds fine, but when I attempt to run the app, I get the following error:

Application startup exception: System.MissingMethodException: Method not found: 'GeoAPI.IGeometryServices NetTopologySuite.NtsGeometryServices.get_Instance()'.
at Microsoft.Extensions.DependencyInjection.SqlServerNetTopologySuiteServiceCollectionExtensions.AddEntityFrameworkSqlServerNetTopologySuite(IServiceCollection serviceCollection)
at Microsoft.EntityFrameworkCore.SqlServer.Infrastructure.Internal.SqlServerNetTopologySuiteOptionsExtension.ApplyServices(IServiceCollection services)
at Microsoft.EntityFrameworkCore.Internal.ServiceProviderCache.ApplyServices(IDbContextOptions options, ServiceCollection services)
at Microsoft.EntityFrameworkCore.Internal.ServiceProviderCache.<>c__DisplayClass4_0.<GetOrAdd>b__2(Int64 k)
at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
at Microsoft.EntityFrameworkCore.Internal.ServiceProviderCache.GetOrAdd(IDbContextOptions options, Boolean providerRequired)
at Microsoft.EntityFrameworkCore.DbContext..ctor(DbContextOptions options)
at Entities.CRFlowContext..ctor(DbContextOptions`1 options) in C:\Src\myproj\myproj.Entities\MyProjContext.cs:line 11
at lambda_method(Closure )
at Microsoft.EntityFrameworkCore.Internal.DbContextPool`1.Rent()
at Microsoft.EntityFrameworkCore.Internal.DbContextPool`1.Lease..ctor(DbContextPool`1 contextPool)
Run Code Online (Sandbox Code Playgroud)

I suspect this has to do with creating my entity in the Program.cs and injecting it into the rest of the app.

Any ideas on how to fix this?

小智 12

您需要删除 NetTopologySuite 库并仅使用Microsoft.EntityFrameworkCore.SqlServer.NetTopologySuite.


Sco*_*tie 5

对于任何对此感兴趣的人,您都需要安装NetTopologySuite.Core而不是NetTopologySuite。

一旦我安装了它,它就可以正常工作。