【问题标题】:Cannot read SqlGeography with Dapper无法使用 Dapper 读取 SqlGeography
【发布时间】:2017-01-04 08:30:44
【问题描述】:

我关注了很多 SO 问题,我遇到了类似的错误,但我无法解决这个问题。

我的 Dapper 版本是 1.50.1 我的 Microsoft.SqlServer.Types 版本是 11.0.2

我在配置文件中添加了绑定重定向,如下:

<runtime>
    <dependentAssembly>
      <assemblyIdentity name="Microsoft.SqlServer.Types" publicKeyToken="89845dcd8080cc91" culture="neutral" />
      <bindingRedirect oldVersion="10.0.0.0" newVersion="11.0.0.0" />
    </dependentAssembly>
</runtime>

我得到的错误是:

解析第 2 列时出错(位置=POINT (-122.349 47.651) - 对象)

虽然内部异常是:

[A]Microsoft.SqlServer.Types.SqlGeography 无法转换为 [B]Microsoft.SqlServer.Types.SqlGeography。类型 A 源自 'Microsoft.SqlServer.Types, Version=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91' 在位置 'C:\WINDOWS\assembly\GAC_MSIL\Microsoft.SqlServer.Types\10.0 的上下文 'Default' 中.0.0__89845dcd8080cc91\Microsoft.SqlServer.Types.dll'。类型 B 源自 'Microsoft.SqlServer.Types, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91' 在位置 'C:\WINDOWS\assembly\GAC_MSIL\Microsoft.SqlServer.Types\11.0 的上下文 'Default' 中.0.0__89845dcd8080cc91\Microsoft.SqlServer.Types.dll'。

如果我只是读取动态对象,一切都很好,但如果我读取自己的具有 SqlGeography 的类型,我会收到此错误。

我以为我按照建议做了所有事情,但似乎我错过了一些东西。

【问题讨论】:

标签: dapper sqlgeography


【解决方案1】:

尝试阅读this article 主题SQL CLR 数据类型(几何、地理和hierarchyid)

或者,尝试将其添加到您的 connectionString 中:

“Type System Version=SQL Server 2012”,强制 SqlClient 加载版本 11.0 的程序集

【讨论】:

    【解决方案2】:

    @Marc Gravell 做得很好,请参阅下面的参考资料,它将挽救您的生命。

    SqlGeography

    Issue

    对于 ASP.NET 应用程序,将以下代码行添加到 Global.asax.cs 中的 Application_Start 方法:

    SqlServerTypes.Utilities.LoadNativeAssemblies(Server.MapPath("~/bin"));
    

    对于桌面应用程序,添加以下代码行以在执行任何空间操作之前运行:

    SqlServerTypes.Utilities.LoadNativeAssemblies(AppDomain.CurrentDomain.BaseDirectory);
    

    .LoadNativeAssemblies 方法将根据 CPU 架构为您获取正确的程序集。

    在 bin 文件夹中你应该有这个。

    【讨论】:

      猜你喜欢
      • 2012-08-18
      • 2016-03-18
      • 1970-01-01
      • 1970-01-01
      • 2012-09-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-22
      • 2016-07-01
      相关资源
      最近更新 更多