How to set null to a GUID property

后端 未结 8 1928
春和景丽
春和景丽 2020-12-16 10:25

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

8条回答
  •  无人及你
    2020-12-16 10:49

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

    No. Because it's non-nullable. If you want it to be nullable, you have to use Nullable - if you didn't, there'd be no point in having Nullable 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.

    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.

提交回复
热议问题