SB2*_*055 2 c# linq sql-server entity-framework dapper
我Location在User桌面上配置了EF 字段:
public DbGeography Location { get; set; }
Run Code Online (Sandbox Code Playgroud)
但是当我查询我的User表时:
user = connection.Query<User>("update [User] set LastOnline = @lastOnline output INSERTED.* where Username = @un",
new { lastOnline = DateTime.UtcNow, un = username }).First();
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
消息=解析列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行(IDataReader)Dapper.SqlMapper.Query [T]中的
1.MoveNext() in d:\Dev\dapper-dot-net\Dapper NET40\SqlMapper.cs:line 1572 at System.Collections.Generic.List1..ctor(IEnumerable1 collection) at System.Linq.Enumerable.ToList[TSource](IEnumerable1 source) (IDbConnection cnn,String sql,Object param,IDbTransaction transaction,Boolean buffered,Nullable1 commandTimeout, Nullable1 commandType)在d:\ Dev\dapper-dot-net\Dapper NET40\SqlMapper.cs:第1443行,位于App.Services.BrowseService.GetProfiles(在C:\ PROJECTS\App\App-MAIN\App\Services\BrowseService.cs中的ProfileSearchDto查询,字符串用户名,布尔值isAdmin):第330行InnerException:System.InvalidCastException HResult = -2147467262 Message =无法转换类型为'Microsoft的对象.SqlServer.Types.SqlGeography'键入'System.Data.Entity.Spatial.DbGeography'.
是什么造成的?
更新
只是为了踢,我尝试使用EF:
db.Database.SqlQuery<User>("bla bla")
Run Code Online (Sandbox Code Playgroud)
我得到一个不同的错误:
消息=对象类型<> 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();
Run Code Online (Sandbox Code Playgroud)
要求加载项注册自己.然后它应该工作.如果您遇到程序集不匹配异常,则应该能够使用程序集绑定重定向来解决它.这特别适用于底层SqlGeography类型,其中SQL Server返回的元数据是与Microsoft.SqlServer.Types包中的元数据不同的版本.但是程序集绑定重定向工作正常.