真的很簡單:
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:不存在從物件型別 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這個 nugetdotMorten.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-spatial-types)。
它適用于簡單的點,但在多面體的情況下,我會收到此錯誤。
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
uj5u.com熱心網友回復:
這取決于您使用哪個 SQL Server 提供程式。linq2db支持兩種可用的 ADO.NET 提供程式:
- 因為
System.Data.SqlClient你必須使用dotMorten.Microsoft.SqlServer.Types1.5, - 供
Microsoft.Data.SqlClient使用dotMorten.Microsoft.SqlServer.Types2.5
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/458735.html
下一篇:如何使BitArray只讀?
