簡體   English   中英

如何使用linq2db插入sql地理?

[英]How to use linq2db to insert sql geography?

這真的很簡單:

var a = SqlGeography.STGeomFromText(new SqlChars("POINT(-122.34900 47.65100)"), 4326);`

然后插入

await _geoDb.Table.Value(x => x.Geom, a) // Geom is of type SqlGeography

這給我錯誤:

System.ArgumentException:不存在從 object 類型 Microsoft.SqlServer.Types.SqlGeography 到已知托管提供程序本機類型的映射。

我嘗試使用geography::STGeomFromText手動插入並且按預期工作。

映射是:

[Column("geom"), Nullable] 
public SqlGeography Geom { get; set; } // geography

表定義。

create table municipal.country
(
    id       int identity
        constraint PK_Canton
            primary key,
    geom     geography
)

我正在為Microsoft.SqlServer.Types使用這個 nuget dotMorten.Microsoft.SqlServer.Types.

Linq2Db 版本是:3.1.6(如果我更新到最新版本是一樣的)。

更新

我能夠通過安裝dotMorten.Microsoft.SqlServer.Types 1.5並將該自定義程序集指令添加到 Program.cs 中來解決此問題( https://linq2db.github.io/articles/FAQ.html#how-can-i-use -sql-server-空間類型)。

它適用於簡單點,但在多面體的情況下,我收到此錯誤。

System.InvalidOperationException: Invalid operation. The connection is closed.
   at System.Data.SqlClient.TdsParserStateObject.WritePacket(Byte flushMode, Boolean canAccumulate)
   at System.Data.SqlClient.TdsParserStateObject.WriteBytes(ReadOnlySpan`1 b, Int32 len, Int32 offsetBuffer, Boolean canAccumulate, TaskCompletionSource`1 completion, Byte[] array)
   at System.Data.SqlClient.TdsParserStateObject.WriteByteArray(Byte[] b, Int32 len, Int32 offsetBuffer, Boolean canAccumulate, TaskCompletionSource`1 completion)
   at System.Data.SqlClient.TdsParser.TDSExecuteRPCAddParameter(TdsParserStateObject stateObj, SqlParameter param, MetaType mt, Byte options)
   at System.Data.SqlClient.TdsParser.TdsExecuteRPC(_SqlRPC[] rpcArray, Int32 timeout, Boolean inSchema, SqlNotificationRequest notificationRequest, TdsParserStateObject stateObj, Boolean isCommandProc, Boolean sync, TaskCompletionSource`1 completion, Int32 startRpc, Int32 startParam)
   at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, SqlDataReader ds)
   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite, String method)
   at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, Boolean sendToPipe, Int32 timeout, Boolean asyncWrite, String methodName)
   at System.Data.SqlClient.SqlCommand.BeginExecuteNonQuery(AsyncCallback callback, Object stateObject)
   at System.Threading.Tasks.TaskFactory`1.FromAsyncImpl(Func`3 beginMethod, Func`2 endFunction, Action`1 endAction, Object state, TaskCreationOptions creationOptions)
   at System.Threading.Tasks.TaskFactory`1.FromAsync(Func`3 beginMethod, Func`2 endMethod, Object state)
   at System.Data.SqlClient.SqlCommand.ExecuteNonQueryAsync(CancellationToken cancellationToken)
--- End of stack trace from previous location where exception was thrown ---
   at LinqToDB.Data.RetryPolicy.RetryPolicyBase.ExecuteImplementationAsync[TResult](Func`2 operation, CancellationToken cancellationToken)
   at LinqToDB.Data.DataConnection.ExecuteNonQueryAsync(CancellationToken cancellationToken)
   at LinqToDB.Data.DataConnection.QueryRunner.ExecuteNonQueryAsync(CancellationToken cancellationToken)
   at LinqToDB.Linq.QueryRunner.NonQueryQueryAsync(Query query, IDataContext dataContext, Expression expression, Object[] ps, Object[] preambles, CancellationToken cancellationToken)
   at LinqToDB.Linq.QueryRunner.NonQueryQueryAsync(Query query, IDataContext dataContext, Expression expression, Object[] ps, Object[] preambles, CancellationToken cancellationToken)
   at LinqToDB.Linq.ExpressionQuery`1.LinqToDB.Async.IQueryProviderAsync.ExecuteAsync[TResult](Expression expression, CancellationToken cancellationToken)
   at LinqToDB.LinqExtensions.InsertAsync[T](IValueInsertable`1 source, CancellationToken token)
   at GeoIndexer.Services.GeoIndexerService.IndexCantons(CancellationToken cancellationToken) in /Users/nikola/projects/immoledo/GeoIndexer/src/Geoindexer/Services/GeoIndexerService.cs:line 118
   at GeoIndexer.GeoIndexerConsoleApp.<>c__DisplayClass5_0.<<StartAsync>b__1>d.MoveNext() in /Users/nikola/projects/immoledo/GeoIndexer/src/Geoindexer/GeoIndexerConsoleApp.cs:line 41
info: Microsoft.Hosting.Lifetime[0]

更新它有效,如果我不使用insertAsync而只是InsertWithInt32Identity

這取決於您使用哪個 SQL 服務器提供商。 linq2db支持兩個可用的 ADO.NET Provider:

  • 對於System.Data.SqlClient你必須使用dotMorten.Microsoft.SqlServer.Types 1.5
  • 對於Microsoft.Data.SqlClient使用dotMorten.Microsoft.SqlServer.Types 2.5

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM