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
table:
上为 EF 配置了 Location
字段
public DbGeography Location { get; set; }
然而,当我查询我的 User
table 时:
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__111.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
秃顶
结论
我的应用程序需要按半径搜索,目前使用简单的 lat/long 框式查询。我试图升级我的实现以使用空间类型,但看起来我的工具不支持这种情况。回到天真我去。
Dapper 在核心库中不支持 Entity Framework 类型 ,以减少依赖项的数量。但是,它有一个可扩展的类型处理程序模型,DbGeography
的绑定包含在 Dapper.EntityFramework
包中。添加后,您需要调用:
Dapper.EntityFramework.Handlers.Register();
要求加载项自行注册。然后它应该工作。如果您遇到程序集不匹配异常,您应该能够使用程序集绑定重定向来解决它。这尤其适用于基础 SqlGeography
类型,其中 SQL 服务器 returns 的元数据与 Microsoft.SqlServer.Types
包中的元数据版本不同。但是程序集绑定重定向工作正常。
我在 User
table:
Location
字段
public DbGeography Location { get; set; }
然而,当我查询我的 User
table 时:
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
秃顶
结论
我的应用程序需要按半径搜索,目前使用简单的 lat/long 框式查询。我试图升级我的实现以使用空间类型,但看起来我的工具不支持这种情况。回到天真我去。
Dapper 在核心库中不支持 Entity Framework 类型 ,以减少依赖项的数量。但是,它有一个可扩展的类型处理程序模型,DbGeography
的绑定包含在 Dapper.EntityFramework
包中。添加后,您需要调用:
Dapper.EntityFramework.Handlers.Register();
要求加载项自行注册。然后它应该工作。如果您遇到程序集不匹配异常,您应该能够使用程序集绑定重定向来解决它。这尤其适用于基础 SqlGeography
类型,其中 SQL 服务器 returns 的元数据与 Microsoft.SqlServer.Types
包中的元数据版本不同。但是程序集绑定重定向工作正常。