简体   繁体   English

C#,WCF和DateTime.MinValue()与Null Date

[英]C#, WCF and DateTime.MinValue() vs. Null Date

I have searched around but have yet to find a satisfying answer for my issue: 我已经四处搜索但尚未找到令人满意的答案:

Overview: I am working on a small app that uses several DateTime fields into a single table using WCF. 概述:我正在开发一个小应用程序,它使用WCF将多个DateTime字段用于单个表。 We are trying to eliminate null fields in all of our tables. 我们正试图消除所有表中的空字段。 Up until now, whenever we are converting a datetime field selected from a table we first verify that it is null before we display or otherwise "use" it. 到目前为止,每当我们转换从表中选择的日期时间字段时,我们首先在显示或“使用”它之前验证它是否为空。 If the value is NULL, we substitute DateTime.MinValue for the value. 如果值为NULL,我们将DateTime.MinValue替换为值。 Since we are now removing the nullable aspect of all fields, we need to insert a common value representing null; 由于我们现在正在删除所有字段的可空方面,我们需要插入一个表示null的公共值; since DateTime.MinValue() is substituted everywhere in the code, it seems like a viable value to put into the field as a null substitute. 因为DateTime.MinValue()在代码中的每个地方被替换,所以作为空替换放入字段似乎是一个可行的值。

The problem: Inserting a DateTime.MinValue() causes the generic "problem executing this request" error. 问题:插入DateTime.MinValue()会导致通用“执行此请求时出现问题”错误。

Solution? 解? : As has been documented elsewhere, the DateTime.MinValue() has an unspecified DateTimeKind so... we add the ToUniversalTime call, as in : :正如其他地方所记载的那样, DateTime.MinValue()有一个未指定的DateTimeKind所以...我们添加ToUniversalTime调用,如:

DateTime nullDate = DateTime.MinValue.ToUniversalTime(); // 1/1/0001 6:00:00 AM

This doesn't work, perhaps because of some "acceptable range" setting within Date conversions on WCF? 这不起作用,可能是因为在WCF上的日期转换中有一些“可接受的范围”设置?

Noted in the comment is the resulting value of "1/1/0001 6:00 am". 注释中注明的是“1/1/0001 6:00 am”的结果值。 The odd 6:00am or the year "1" aside, that seems fine. 奇怪的是早上6点或一年“1”,这似乎很好。 (Considering the past of default years like 12:00 AM 1970 are still fairly standard, but I digress...) (考虑到过去的默认年份,比如1970年代上午12:00仍然是相当标准的,但我离题了...)

Yes, I know that DateTime is not C#, it's .NET. 是的,我知道DateTime不是C#,它是.NET。 I'm also aware that WCF has some form of "min" and "max" times allowed within its conversion constraints. 我也知道WCF在其转换限制内允许某种形式的“min”和“max”次。 I don't know how to set those (nor do I know how to set the VerboseMessages bit; I'm fairly new to WCF.) 我不知道如何设置它们(我也不知道如何设置VerboseMessages位;我对WCF很新。)

The suggestion to manually create a new field value that converts nicely into the table is viable, and it works: 手动创建一个可以很好地转换为表的新字段值的建议是可行的,它可以工作:

DateTime nullDate = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);

(BTW, If we use the year 1 instead of 1970 (for example) the insert will also fail.) (顺便说一句,如果我们使用第1年而不是1970年(例如),插入也将失败。)

Even if we were to use the above nullDate as a workable substitute... the problem is that everywhere in the code we are still evaluating against DateTime.MinValue() representing a null date, and those two dates aren't quite the same. 即使我们使用上面的nullDate作为可行的替代品......问题是代码中的任何地方我们仍然在针对表示空日期的DateTime.MinValue()评估,并且这两个日期并不完全相同。

The only viable solution that I can think of is to override or otherwise create an extended class from DateTime to create a common NullDate and modify all the code accordingly. 我能想到的唯一可行的解​​决方案是覆盖或以其他方式从DateTime创建扩展类以创建公共NullDate并相应地修改所有代码。

Does anyone see a good alternative? 有没有人看到一个很好的选择? Does anyone know a solution to inserting the System.DateTime.MinValue() into a WCF table such as altering the acceptable boundries of a good/bad date? 有没有人知道将System.DateTime.MinValue()插入WCF表的解决方案,例如更改好/坏日期的可接受边界?

I feel like I'm going to have to bite the bullet and change all references to MinValue... I'm trying to avoid that because it doesn't follow any sort of standard logical thought in evaluating "default" values. 我觉得我必须咬紧牙关并改变对MinValue的所有引用......我试图避免这种情况,因为它在评估“默认”值时没有遵循任何标准的逻辑思想。

Suggestions anyone? 有人建议吗?

It depends on the database you are using to store the data. 它取决于您用于存储数据的数据库。 SQL Server, for example, has a minimum date of 1/1/1753, see MSDN . 例如,SQL Server的最小日期为1/1/1753,请参阅MSDN I am not sure about Oracle. 我不确定Oracle。

If you Have to use a magic date, you can use that, or something else specific (I use 10 December 1815 in honor of the Lady Ada Lovelace, the first programmer). 如果你必须使用魔法日期,你可以使用它或其他特定的东西(我使用1815年12月10日,以纪念第一个程序员Lady Ada Lovelace)。 Generally speaking, if you have a situation where you have no known value, the data store should represent the value as 'null.' 一般来说,如果您遇到没有已知值的情况,则数据存储应将值表示为“null”。 Now in practice, that's not always viable. 现在在实践中,这并不总是可行的。 What you could do is refactor the nullable columns into a subtable, and only include a child record when you in fact have something to record. 您可以做的是将可空列重构为子表,并且只有当您实际上有要记录的内容时才包含子记录。

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

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