简体   繁体   English

对于ORM对象为空

[英]Nullable for ORM Objects

Based on reading Nullable Types , I understand that it's proper to use Nullable for a primitive type that may be null in a database. 通过阅读Nullable Types ,我了解对数据库中可能为null的基本类型使用Nullable是适当的。

Is it necessary to use the ? 是否有必要使用? (Nullable) type for objects? (空)对象的类型?

Example: 例:

public DateTime? DateCreated {get; set; }

or 要么

 public DateTime DateCreated {get; set; }

Your example is correct. 您的例子是正确的。 You will need DateTime? 您将需要DateTime? to have the ORM (say NH) treat it as a nullable type. 使ORM(例如NH)将其视为可为空的类型。 For objects you dont need to put the question mark. 对于对象,您不需要添加问号。

Also, on a side-note, the question mark T? 另外,在旁注中,问号T? is an alias for Nullable<T> . Nullable<T>的别名。 The compiler will translate the T? 编译器会翻译T? to Nullable<T> . Nullable<T> You can check this on the debugger if you want a proof :) 如果需要证明,可以在调试器上检查:

In your example, Nullable<T> is valid as DateTime is not a reference type. 在您的示例中, Nullable<T>有效,因为DateTime不是引用类型。 However, for objects (and types derived from object), you cannot use Nullable<T> as the compiler will throw an error; 但是,对于对象(以及从对象派生的类型),不能使用Nullable<T>因为编译器会抛出错误。 see: C# nullable string error 请参阅: C#可为空的字符串错误

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

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