如何处理System.InvalidOperationException:DataReader.GetFieldType(13)?(再次) [英] How to deal with System.InvalidOperationException: DataReader.GetFieldType(13)? (again)

查看:74
本文介绍了如何处理System.InvalidOperationException:DataReader.GetFieldType(13)?(再次)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将我的ASP.NET Webforms应用程序升级到.NET 4.7.2(和4.8)并将基础数据库服务器更改为SQL Server 2017之后,我开始遇到此异常:

After upgrading my ASP.NET webforms application to .NET 4.7.2 (and 4.8) and changing the underlying database server to SQL Server 2017, I started to get this exception:

异常详细信息:System.InvalidOperationException:DataReader.GetFieldType(13)返回null.

Exception Details: System.InvalidOperationException: DataReader.GetFieldType(13) returned null.

数据集中第13列的基本数据类型为 GEOGRAPHY .我对可能的原因进行了一些研究,这是我尝试过但未成功的事情.

Underlying datatype of column 13 in the dataset is GEOGRAPHY. I have researched a bit about possible causes and this are things I have tried without success.

  1. 计算机应已安装SQL Server实例.我有在我的开发计算机上,我有SQL Server 2017 Express,生产"中有针对大型企业的SQL Server 2017.所以,这不是罪魁祸首.

  1. The machine should have an instance of SQL Server installed. I have it. On my dev computer I have SQL Server 2017 Express and the "production" has SQL Server 2017 for the big boys. So, this is not the culprit.

建议安装软件包 Microsoft.SqlServer.Types .我已经安装了14.0.1016.290版本.创建了引用,将 Microsoft.SqlServer.Types.dll 复制到bin文件夹中.这不能解决错误.

There are suggestions to install package Microsoft.SqlServer.Types. I have installed version 14.0.1016.290. The reference was created, Microsoft.SqlServer.Types.dll was copied in the bin folder. This has not resolved the error.

另一个建议是将此呼叫放入 Global.asax :

Another suggestion was to put this call into the Global.asax:

public class Global_asax : System.Web.HttpApplication
{
    void Application_Start(object sender, EventArgs e)
    {
        ...
        /* For ASP.NET Web Applications, add the following line of code to the Application_Start method in Global.asax.cs: */
        SqlServerTypes.Utilities.LoadNativeAssemblies(Server.MapPath("~/bin"));
    }
}

  • 最后,我在 web.config 中添加了程序集重定向.我不确定我是否正确使用了publicKeyToken.

  • And as a last resort I have added assembly redirect in a web.config. I am not sure if I got publicKeyToken right.

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

  • 以上均未解决我的问题,这是我在各种论坛和堆栈交换上可以找到的所有建议.我研究了在

    None of the above resolved my problem and this were all the suggestions I could find on various forums and stack exchange. I have looked into the source code of GetFieldType() method found at http://www.dotnetframework.org/default.aspx/4@0/4@0/DEVDIV_TFS/Dev10/Releases/RTMRel/ndp/fx/src/Data/System/Data/SqlClient/SqlDataReader@cs/1305376/SqlDataReader@cs but it offered no clue.

    我曾考虑过以前的.Net版本中可能会存在一些旧残留物,该残留物调用了此方法的错误版本,但我不知道如何检查此方法.我只是觉得微软无法将他们的东西放在一起真是太可惜了.我的应用程序的旧版本只能正常工作.

    I have thought about there might be some old remnant from previous .Net version that calls incorrect version of this method, but I do not know how to check this. I just think it is a shame Microsoft cannot keep their stuff together. Old version of my app just simply worked.

    重要提示:对于所有认为此问题已得到回答的人:并非如此!这是一个长期存在的问题.

    IMPORTANT: To all of you who think this question was already answered: it was not! It is a persistent problem.

    更新-其他信息我之前在开发计算机和生产服务器上的配置是.NET 4.0和SQL Server2008.将数据库升级到SQL Server 2017之后,我仅更改了现有应用程序上的连接字符串,然后它起作用了.实际上,它仍然在生产中使用,因为od .NET的较新版本根本不起作用.

    UPDATE - ADDITIONAL INFORMATION My previous configuration on development computer and production server was .NET 4.0 and SQL Server 2008. After upgrading the database to SQL Server 2017 I changed only the connection strings on existing app and it worked. Actually it is still used in production since newer versions od .NET simply do not work.

    之后,我将开发机升级到.NET 4.8,将实体框架升级到6.4并编译了该应用程序.结果是地理数据类型被破坏了.我已采取所有纠正措施的步骤已在上方.这是怎么了

    After that I upgraded my dev machine to .NET 4.8, entity framework to 6.4 and compiled the app. And as result Geography datatype was broken. All steps I have take to remedy this are above. What is wrong here?

    推荐答案

    您尝试过的解决方案将解决不同的异常,通常是此异常的变体:

    The solutions you have tried will fix a different exception, usually variations of this:

    System.InvalidOperationException:'空间类型和函数不适用于此提供程序,因为找不到程序集'Microsoft.SqlServer.Types'版本10或更高版本.'

    System.InvalidOperationException: 'Spatial types and functions are not available for this provider because the assembly 'Microsoft.SqlServer.Types' version 10 or higher could not be found. '

    如果您没有看到此错误,则可以安全地假定找到了 types 的兼容版本,这通常足以说明将执行空间查询.

    If you are not seeing this error, then we can safely assume that a compatible version of types was found and this is usually enough to say that spatial queries will execute.

    数据类型推荐我不能肯定地说这与此相关,但是为了在EF中使用空间类型获得更大的成功,您应该考虑在数据类(模型)中使用 System.Data.Entity.Spatial.DbGeography ,而不是 System.Spatial.Geography

    Data Type Recommendation I can't say that it is related to this for sure, but for greater success in EF with spatial types you should consider using System.Data.Entity.Spatial.DbGeography in your data classes (model), instead of System.Spatial.Geography

    这仍将解析为 SQL Geography 数据类型.

    要进一步研究,将需要有关数据模型和执行以引发OP所遇到的特定异常的代码的更多信息.没有这些信息,我已经采用了一个正在运行的旧的 commercial ASP.Net OData v4 API项目:

    To furthur investigate would require more information around the data model and the code that executed to raise the specific exception that OP has experienced. Without this information, I have taken an older commercial ASP.Net, OData v4 API project that was running:

    • .Net 4.6.1
    • 实体框架6.2.0
    • Microsoft.Spatial 7.3.1
    • Microsoft.SqlServer.Types 14.0.314.76
    • System.Spatial 5.8.3

    然后我升级了解决方案中每个项目的框架版本和参考,其中有一些NuGet软件包要处理,因此在将每个项目的框架版本更改为4.7.2之后,我使用了此命令:

    I then upgraded the framework version and references for each project in the solution, there were a few NuGet packages to process so I used this command after changing the framework version of each project to 4.7.2:

    Update-Package -Reinstall -IgnoreDependencies
    

    现在该项目如下:

    • .net 4.7.2
    • 实体框架6.4.0
    • Microsoft.Spatial 7.6.2
    • Microsoft.SqlServer.Types 14.0.1016.290
    • System.Spatial 5.8.4

    该项目仍然可以像以前一样工作,我可以从数据库中查询 Paddock ,并可以在结果中看到地理空间字段:

    The project still works as before, I can query a Paddock from my database and can see the geospatial fields in the result:

    • 注意: 在此示例中, Geospatial 字段定义如下:

    public DbGeography Perimeter { get; set; }
    public DbGeography GateLocation { get; set; }
    

    {
        "@odata.context": "http://localhost:1231/odata/$metadata#Paddocks(EntityID,FarmID,PropertyNumber,VicseedID,VicseedCode,PropertyArea,SketchFile,Isolated,Perimeter,GateLocation,State,SketchData,DefaultInspector)/$entity",
        "EntityID": 8242,
        "FarmID": 6065,
        "PropertyNumber": "1",
        "VicseedID": 3002,
        "Isolated": false,
        "Perimeter": {
            "Geography": {
                "CoordinateSystemId": 4326,
                "WellKnownText": "CURVEPOLYGON (CIRCULARSTRING (146.68923684498316 -38.263219203427454, 146.67772453918761 -38.263219203427454, 146.67772382723493 -38.272258363952844, 146.68923755693584 -38.272258363952844, 146.68923684498316 -38.263219203427454))"
            }
        },
        "GateLocation": {
            "Geography": {
                "CoordinateSystemId": 4326,
                "WellKnownText": "POINT (146.68301582336429 -38.263961881396469)"
            }
        }
    }
    

    下一步要检查什么?

    直接在 SQL Management Studio 中运行相同的SQL查询,记录结果并将其发布到您的问题中.

    What to check next?

    Run the same SQL query directly in SQL Management Studio, record the results and publish into your question.

    然后还发布执行相同查询但引发捕获的异常的代码.

    Then also publish the code that executes the same query but raises the captured exception.

    经过全面的回归测试后,我在API或前端中都找不到新的异常,因此我有信心建议升级到 .Net 4.7.2 实体框架6.4

    After full regression testing, I can find no new anomolies in the API or the front end and from this I have the personal confidence to reccomend upgrading both to .Net 4.7.2 and Entity Framework 6.4

    这篇关于如何处理System.InvalidOperationException:DataReader.GetFieldType(13)?(再次)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆