简体   繁体   English

如何从LINQ to SQL中的SQL Server 2012识别FormatException的来源

[英]How to identify source of a FormatException from SQL Server 2012 in LINQ to SQL

I have a DBML file that defines a large number of entities for use by our LINQ-to-SQL queries. 我有一个DBML文件,该文件定义了供LINQ-to-SQL查询使用的大量实体。 It has been working fine on SQL Server 2005 and SQL Server 2008 and a particular version of third party software with which we are integrating. 它在SQL Server 2005和SQL Server 2008以及我们正在与之集成的特定版本的第三方软件上运行良好。 However, we are now investigating the newer version of the third party software running on a new OS (Windows Server 2012) and database platform (SQL Server 2012), and as a 64-bit process. 但是,我们现在正在研究在新OS(Windows Server 2012)和数据库平台(SQL Server 2012)上以及作为64位进程运行的第三方软件的较新版本。

When I try to retrieve an object from one of the tables we have defined in the database (not the third party software) in this new environment, I am getting the exception, "String must be exactly one character long." 当我尝试从我们在这个新环境中的数据库(而不是第三方软件)中定义的表的一个获取对象,我得到异常,“字符串必须是一个字符长。” Now I know from other questions and articles that there has been some buggy behavior in the past with Visual Studio creating certain columns as type Char instead of String for an nvarchar(1) column. 现在,从其他问题和文章中我知道,Visual Studio过去将某些列创建为Char类型而不是String为nvarchar(1)列创建了一些错误行为。 But there are a number of factors leading me to believe I'm dealing with a different problem here: 但是有很多因素使我相信我正在这里处理另一个问题:

  1. The table from which I am retrieving data, according to the DBML file, has no Char columns in it and doesn't even have a nvarchar(1) database column. 根据DBML文件,我从中检索数据的表中没有Char列,甚至没有nvarchar(1)数据库列。
  2. Although the schema of the third party tables have changed, our database schema and DBML file have not changed between the version that works and the one that throws this exception. 尽管第三方表的架构已更改,但在起作用的版本和引发此异常的版本之间,我们的数据库架构和DBML文件并未更改。 And I'm only querying data from one of our tables ( Dim dtl = Aggregate i In context.FSE_ItemDetails Into First() ). 而且我只从其中一个表中查询数据( Dim dtl = Aggregate i In context.FSE_ItemDetails Into First() )。

What I need help with is determining the table and column that's causing this error. 我需要帮助的是确定导致此错误的表和列。 I'm assuming that the error was from the table I'm trying to retrieve data from, but as I look at the schema it's hard to imagine how. 我假设错误是来自我要从中检索数据的表,但是当我查看架构时,很难想象它是怎么回事。 A FormatException has very little information about the source of the error, and by the time I catch it, I don't have access to much about what was going on. FormatException很少提供有关错误源的信息,而当我捕获到该错误时,我没有太多了解发生了什么情况的信息。 I can see in the call stack (provided only as a string, unfortunately), that System.Convert.ToChar is the function that threw it, and our ItemDetail.get_Item() generated function is the last level of our code that was in the stack. 我可以在调用堆栈(不幸的是,仅以字符串形式提供)中看到, System.Convert.ToChar是抛出该函数的函数,而我们的ItemDetail.get_Item()生成的函数是代码中的最后一层堆。 But there's no hint of a column. 但是没有专栏的提示。 I would like, if possible, to narrow this down by changing my test program that reproduces the error because I don't have a debugging environment set up on the same environment where I can reproduce the error. 如果可能,我希望通过更改重现错误的测试程序来缩小此范围,因为我没有在可以重现错误的环境中设置调试环境。 But I don't know how to access the data in the call stack programmatically once the exception has unwound up to my level. 但是,一旦异常解除到我的水平,我不知道如何以编程方式访问调用堆栈中的数据。

EDIT 编辑

I was mistaken about the fact that I was only accessing the one table. 我只访问一个表这一事实使我感到误解。 I forgot that my test output was referring to an associated object from the third party schema, which indeed may have been updated. 我忘记了我的测试输出是引用了第三方架构中的关联对象,该对象确实可能已更新。 The question remains, however, is there a good way to identify the source column causing the error. 问题仍然存在,但是,有没有一种好的方法来识别导致错误的源列。 The table has many, many columns in it. 该表中有很多列。

After discovering that I was looking at the wrong table I just opened up the DBML file in an XML editor, located the definition of the table I now suspected was the problem, and looked for each Column element whose Type was set to System.Char . 在发现我查看了错误的表之后,我只是在XML编辑器中打开了DBML文件,找到了我现在怀疑是问题所在的表的定义,并查找了Type设置为System.Char每个Column元素。 I queried each column in the new database and finally found one that did not contain exactly 1 character. 我查询了新数据库中的每一列,最后找到了一个不完全包含1个字符的列。 Inspecting it further showed that it used to be defined as Char(1) NULL , but is now nvarchar(2) NULL . 对其进行进一步检查表明,它曾经被定义为Char(1) NULL ,但是现在是nvarchar(2) NULL I have confirmed this was the source of the problem I was seeing by correcting it and running the test again without error. 通过更正并再次运行测试而没有错误,我已经确认这是我所看到的问题的根源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM