用Dapper

时间:2017-01-04 08:30:44

标签: dapper sqlgeography

我跟着很多SO问题而且我遇到了simillar错误,但我无法解决这个问题。

我的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列时出错(Location = POINT(-122.349 47.651) - Object)

虽然内部例外是:

  

[A] Microsoft.SqlServer.Types.SqlGeography无法强制转换为[B] Microsoft.SqlServer.Types.SqlGeography。类型A源自&#39; Microsoft.SqlServer.Types,Version = 10.0.0.0,Culture = neutral,PublicKeyToken = 89845dcd8080cc91&#39;在上下文中&#39;默认&#39;在位置&#39; C:\ WINDOWS \ assembly \ GAC_MSIL \ Microsoft.SqlServer.Types \ 10.0.0.0__89845dcd8080cc91 \ Microsoft.SqlServer.Types.dll&#39;。类型B源自&#39; Microsoft.SqlServer.Types,Version = 11.0.0.0,Culture = neutral,PublicKeyToken = 89845dcd8080cc91&#39;在上下文中&#39;默认&#39;在位置&#39; C:\ WINDOWS \ assembly \ GAC_MSIL \ Microsoft.SqlServer.Types \ 11.0.0.0__89845dcd8080cc91 \ Microsoft.SqlServer.Types.dll&#39;。

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

我以为我按照建议完成了所有工作,但似乎我错过了一些东西。

2 个答案:

答案 0 :(得分:1)

尝试阅读有关主题 SQL CLR数据类型(几何,地理位置和hierarchyid)this article

或者,尝试在connectionString中添加它:

&#34;键入系统版本= SQL Server 2012&#34;,强制SqlClient加载程序集的版本11.0

答案 1 :(得分:0)

@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文件夹中你应该有这个。

enter image description here