2016-09-11 42 views
1

我已經配置了Location場在我User表EF:小巧玲瓏無法投型「Microsoft.SqlServer.Types.SqlGeography」的對象鍵入「System.Data.Entity.Spatial.DbGeography」

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(); 

我得到以下錯誤:

Message=Error parsing column 122 (Location=POINT (-118.2436849 34.0522342) - Object) Source=Dapper StackTrace: at Dapper.SqlMapper.ThrowDataException(Exception ex, Int32 index, IDataReader reader, Object value) in d:\Dev\dapper-dot-net\Dapper NET40\SqlMapper.cs:line 4045 at Deserialize4650b5f0-d037-49ad-802e-8a9be95e8496(IDataReader) at Dapper.SqlMapper.d__11 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 source) at Dapper.SqlMapper.Query[T](IDbConnection cnn, String sql, Object param, IDbTransaction transaction, Boolean buffered, Nullable 1 commandTimeout, Nullable 1 commandType) in d:\Dev\dapper-dot-net\Dapper NET40\SqlMapper.cs:line 1443 at App.Services.BrowseService.GetProfiles(ProfileSearchDto query, String username, Boolean isAdmin) in c:\PROJECTS\App\App-MAIN\App\Services\BrowseService.cs:line 330 InnerException: System.InvalidCastException HResult=-2147467262 Message=Unable to cast object of type 'Microsoft.SqlServer.Types.SqlGeography' to type 'System.Data.Entity.Spatial.DbGeography'.

這是什麼原因造成的?

更新

只是踢,我嘗試使用EF:

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

我也得到一個不同的錯誤:

Message=No mapping exists from object type <>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]] to a known managed provider native type. Source=System.Data

去禿

結論

我的應用程序需要按半徑進行搜索,並且當前使用天真的緯度/長方框查詢。我試圖將我的實現升級爲使用空間類型,但看起來我的工具不支持這種情況。回到天真的我去。

+0

http://stackoverflow.com/questions/23186832/entity-framework-sqlgeography-vs-dbgeography – Oluwafemi

+0

@Oluwafemi - 我明白了。所以......我如何讓EF和Dapper在上面的查詢中表現出色? – SB2055

+0

EF應處理地理類型(但我根本不使用它們)。最後一個錯誤似乎與地理不相關(但是在使用地理信息時它可能是一個EF錯誤)。如果使用'context.Users.ToList()','User'是否正常工作? – bubi

回答

6

Dapper不支持核心庫中的實體框架類型,以減少依賴關係的數量。但是,它具有可擴展的類型處理程序模型,Dapper.EntityFramework包中包含DbGeography的綁定。一旦你添加了,你需要調用:

Dapper.EntityFramework.Handlers.Register(); 

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

+0

完美地工作。我們現在在'空間'上!謝謝馬克。 – SB2055

+0

更多步驟和解釋:https://github.com/StackExchange/Dapper/issues/570 – JasonCoder