我已經配置了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(IEnumerable1 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, Nullable1 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
去禿
結論
我的應用程序需要按半徑進行搜索,並且當前使用天真的緯度/長方框查詢。我試圖將我的實現升級爲使用空間類型,但看起來我的工具不支持這種情況。回到天真的我去。
http://stackoverflow.com/questions/23186832/entity-framework-sqlgeography-vs-dbgeography – Oluwafemi
@Oluwafemi - 我明白了。所以......我如何讓EF和Dapper在上面的查詢中表現出色? – SB2055
EF應處理地理類型(但我根本不使用它們)。最後一個錯誤似乎與地理不相關(但是在使用地理信息時它可能是一個EF錯誤)。如果使用'context.Users.ToList()','User'是否正常工作? – bubi