Visual Studio 数据集设计器 Null
Visual Studio 2008 有一个错误,即无法使用数据集设计器将 int 类型字段设置为可为空。 (有错误报告可以追溯到 Visual Studio 2005,但似乎这个问题从未得到解决。)
唯一可选择的行为是发出在从数据库传回空值时引发异常的代码,而不是将该值设置为null,这将要求数据类型为“int”?而不是“int”。 现实世界的示例情况是当外键为空时,因为尚未分配依赖对象。
除了 1、不使用 Visual Studio 数据集、2. 将数据类型更改为字符串(允许 null,但否定强类型并强制进行大量转换)或 3. 更改发出的代码(这将是如果通过设计器对数据集进行任何更改,则会被覆盖)。
Visual Studio 2008 has a bug in that you cannot use the dataset designer to set an int type field to be nullable. (There are error reports going back to Visual Studio 2005 but it seems this has never been addressed.)
The only selectable behaviour is to emit code that raises an exception if an null value is passed back from the database, rather than set the value to null, which would require the data type to be "int?" instead of "int".
A real world example case is when a foreign key is null because the dependent object has not yet be assigned.
Are there any sensible workarounds other than 1, not using Visual Studio datasets, 2. changing the datatype to string (which allows null but negates the point of strong typing and forces numerous casting), or 3. changing the emitted code (which will be overwritten if any changes are made to the dataset via the designer).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试 此链接。
我怀疑作者经历了许多与您相同的困难,包括手动更新数据集设计器代码(刚刚重新生成)和部分类的使用。
在 VS2008 中对他有用的是手动编辑 DataSet .xsd 文件,为他需要修复的列添加以下值:
这样做是返回 -1 代替 null,没有错误。
作者给出了一个像这样编辑的完整列的例子:
当编辑和保存DataSet时,这些更新不会丢失。
公平警告:我从未尝试过这段代码。有人在作者的博客上评论说这在 VS2008 中对他不起作用(请查找“Tom”发布的第二条评论)。不过,汤姆确实提出了一个解决方案。
我希望它对你有用。
Try this link.
I suspect the author jumped through a lot of the same hoops you've had to, including updating by hand the DataSet designer code (which just gets regenerated) and the use of partial classes.
What worked for him in VS2008 was to edit the DataSet .xsd file manually to add the following values for the columns that he needed to fix:
What this does is return -1 in place of null with no errors.
The author gives an example of a complete column edited like this:
These updates are not lost when the DataSet is edited and saved.
Fair warning: I've never tried this code. Someone commented to the author's blog by saying this did not work for him in VS2008 (look for the second comment posted by "Tom"). Tom does present a fix though.
I hope it works for you.
connect.microsoft.com 主题上建议了一种解决方法 (类型化数据集的可为空类型,作者:Danila Korablin),它演示了一种为修复数据集提供可用替代方案的黑客方法。
简而言之,它使用 myDataSet.cs 模块中的分部类作为向行类添加可为空属性的存储库。您可以在代码中访问这些属性,但不能在设计器中访问。这种添加属性的方式也无法替代原来破损的属性。
“数据源”窗口也不会显示您以这种方式添加到数据集类的属性。如果您尝试通过添加正确的可为空类型的列来修复 MS 生成的数据表类,则数据源可视化工具无法理解它们,从而使数据集无效,因此它不会显示任何内容。这意味着您无法使用 Visual Studio 的内置设计器来访问正确类型的列。
这一缺陷使得像复制包含可为空列的行这样简单的操作变得不可能。
我可以验证 Visual Studio 2005 的数据集设计器不支持可为 null 的列。微软一再明确表示他们不会解决这个问题。
There was a workaround suggested on the connect.microsoft.com topic (Nullable types for typed dataset by Danila Korablin) that illustrates a hack that provides a usable alternative to fixing the dataset.
To put it succinctly, it uses the partial classes, in the myDataSet.cs module, as a repository for adding nullable properties to the row class. You can access these properties in your code but not in the designer. This manner of adding properties also cannot replace the original broken ones.
The Data Sources window also will not show properties you add to dataset classes in this manner. If you try to repair the MS-generated data table class by adding columns of the correct nullable types, the Data Sources visualizer doesn't understand them, invalidating the dataset, so it displays nothing. This means you are prevented from using Visual Studio's built-in designer to access columns of the correct types.
This deficiency makes an operation as simple as copying a row that contains a nullable column impossible.
I can verify nullable columns are unsupported in Visual Studio 2005's dataset designer. Microsoft has repeatedly made it clear they will not address this problem.