簡體   English   中英

Dapper無法將“Microsoft.SqlServer.Types.SqlGeography”類型的對象強制轉換為“System.Data.Entity.Spatial.DbGeography”

[英]Dapper unable to cast object of type 'Microsoft.SqlServer.Types.SqlGeography' to type 'System.Data.Entity.Spatial.DbGeography'

我在User表上配置了EF的Location字段:

 public DbGeography Location { get; set; }

但是當我查詢我的User表時:

 user = connection.Query<User>("update [User] set LastOnline = @lastOnline output INSERTED.* where Username = @un",
                        new { lastOnline = DateTime.UtcNow, un = username }).First();

我收到以下錯誤:

消息=解析列122時出錯(Location = POINT(-118.2436849 34.0522342) - Object)Source = Dapper StackTrace:在d:\\ Dev \\ dapper-中的Dapper.SqlMapper.ThrowDataException(Exception ex,Int32 index,IDataReader reader,Object value) dot-net \\ Dapper NET40 \\ SqlMapper.cs:Dapper.SqlMapper.d__11中的Deserialize4650b5f0-d037-49ad-802e-8a9be95e8496(IDataReader)的第4045行1.MoveNext() in d:\\Dev\\dapper-dot-net\\Dapper NET40\\SqlMapper.cs:line 1572 at System.Collections.Generic.List 1 collection) at System.Linq.Enumerable.ToList[TSource](IEnumerable 1.MoveNext() in d:\\Dev\\dapper-dot-net\\Dapper NET40\\SqlMapper.cs:line 1572 at System.Collections.Generic.List 1..ctor(IEnumerable 1 collection) at System.Linq.Enumerable.ToList[TSource](IEnumerable 1.MoveNext() in d:\\Dev\\dapper-dot-net\\Dapper NET40\\SqlMapper.cs:line 1572 at System.Collections.Generic.List 1 collection) at System.Linq.Enumerable.ToList[TSource](IEnumerable 1 source)Dapper.SqlMapper.Query [T] (IDbConnection cnn,String sql,Object param,IDbTransaction事務,Boolean buffered,Nullable 1 commandTimeout, Nullable 1 commandType)在d:\\ Dev \\ dapper-dot-net \\ Dapper NET40 \\ SqlMapper.cs:App.Services中的第1443行。在c:\\ PROJECTS \\ App \\ App-MAIN \\ App \\ Services \\ BrowseService.cs中BrowseService.GetProfiles(ProfileSearchDto查詢,字符串用戶名,布爾isAdmin):第330行InnerException:System.InvalidC astException HResult = -2147467262 Message =無法將類型為“Microsoft.SqlServer.Types.SqlGeography”的對象強制轉換為“System.Data.Entity.Spatial.DbGeography”。

是什么造成的?

更新

只是為了踢,我嘗試使用EF:

db.Database.SqlQuery<User>("bla bla")

我得到一個不同的錯誤:

消息=對象類型<> f__AnonymousTypef`2 [[System.DateTime,mscorlib,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089],[System.String,mscorlib,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089]]到已知的托管提供程序本機類型。 來源= System.Data

禿頭

結論

我的應用程序需要按半徑搜索,目前使用的是天真的lat / long盒裝查詢。 我試圖升級我的實現以使用空間類型,但看起來我的工具不支持這種情況。 回到天真,我走了。

Dapper不支持核心庫中的實體框架類型,以減少依賴項的數量。 但是,它具有可擴展的類型處理程序模型, DbGeography綁定包含在Dapper.EntityFramework包中。 添加完之后,您需要致電:

Dapper.EntityFramework.Handlers.Register();

要求加載項注冊自己。 然后它應該工作。 如果您遇到程序集不匹配異常,則應該能夠使用程序集綁定重定向來解決它。 這尤其適用於底層的SqlGeography類型,其中SQL Server返回的元數據是與Microsoft.SqlServer.Types包中的元數據不同的版本。 但是程序集綁定重定向工作正常。

暫無
暫無

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

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