[英]How to check if a property of an Entity Framework type is Nullable
I have a EntityDataModel generated from my database. 我有一个从数据库生成的EntityDataModel。 One of the Entity models has two properties that are both string types. 实体模型之一具有两个均为字符串类型的属性。 One is Nullable=True and the other Nullable=False 一个是Nullable = True,另一个是Nullable = False
How do I check the value of the Nullable property during runtime ? 如何在运行时检查Nullable属性的值?
If your properties are decorated with attributes like [Required]
or [StringLength]
with a property MinimumLength
set to a value greater then 0, you could use that property's GetType() method. 如果您的属性装饰有[Required]
或[StringLength]
的属性,并且属性MinimumLength
的值设置为大于0,则可以使用该属性的GetType()方法。 This method will return an object of type Type
and it has a number of other methods like GetCustomAttributes
. 此方法将返回Type
的对象,并且它具有许多其他方法,例如GetCustomAttributes
。 This method will return all the custom attributes applied to your property. 此方法将返回应用于属性的所有自定义属性。
As I said earlier, if you know which attributes were applied, like the ones mentioned, then using YourObject.YourProperty.GetType().GetCustomAttributes(true)
will do the trick. 就像我之前说的,如果您知道应用了哪些属性(如提到的属性),则使用YourObject.YourProperty.GetType().GetCustomAttributes(true)
可以解决问题。 You will need to go through the array and cast the result to the proper attribute. 您将需要遍历数组并将结果转换为适当的属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.