如何为GUID属性设置null [英] How to set null to a GUID property

查看:681
本文介绍了如何为GUID属性设置null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类型为Employee的对象,该对象具有Guid属性.我知道是否要设置为null我必须将我的type属性定义为可为null的Nullable<Guid> prop或Guid吗?道具.

I have an object of type Employee which has a Guid property. I know if I want to set to null I must to define my type property as nullable Nullable<Guid> prop or Guid? prop.

但是在我的情况下,我无法更改道具的类型,因此它仍将保留为Guid类型以及我的同事,我不想使用Guid.Empty.

But in my case I'm not able to change the type of the prop, so it will remains as Guid type and my colleague and I we don't want to use the Guid.Empty.

有没有一种方法可以将我的属性设置为null或string.empty,以便将数据库中的字段重新设置为null.

Is there a way to set my property as null or string.empty in order to restablish the field in the database as null.

我有一种将string.empty转换为null的机制,但是如果将其更改为接受一个空的guid,我将进行很多更改.

I have a mechanism to transform from string.empty to null but I will change many things if the would change to accept a empty guid to null.

请帮忙!

推荐答案

有没有一种方法可以将我的属性设置为null或string.empty,以便将数据库中的字段重新设置为null.

Is there a way to set my property as null or string.empty in order to restablish the field in the database as null.

不.因为它是不可为空的.如果希望它可以为空,则必须使用Nullable<Guid>-否则,没有必要使用Nullable<T>作为开头.在这里,您遇到了一个基本问题-根据您的第一段,您实际上已经知道了.您已经说过:我知道我是否想获得A,我必须要做B-但我想不通过B就获得A." 按照定义是不可能的.

No. Because it's non-nullable. If you want it to be nullable, you have to use Nullable<Guid> - if you didn't, there'd be no point in having Nullable<T> to start with. You've got a fundamental issue here - which you actually know, given your first paragraph. You've said, "I know if I want to achieve A, I must do B - but I want to achieve A without doing B." That's impossible by definition.

您可以得到的最接近的结果是使用一个特定的GUID代表空值-Guid.Empty(在适当的情况下也可以作为default(Guid)使用,例如,用于可选参数的默认值)是明显的候选者,但是您因未指明的原因而拒绝了.

The closest you can get is to use one specific GUID to stand in for a null value - Guid.Empty (also available as default(Guid) where appropriate, e.g. for the default value of an optional parameter) being the obvious candidate, but one you've rejected for unspecified reasons.

这篇关于如何为GUID属性设置null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆