How to set null to a GUID property

后端 未结 8 1898
春和景丽
春和景丽 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:40

    You can use typeof(Guid), "00000000-0000-0000-0000-000000000000" for DefaultValue of the property.

    0 讨论(0)
  • 2020-12-16 10:44

    Choose your poison - if you can't change the type of the property to be nullable then you're going to have to use a "magic" value to represent NULL. Guid.Empty seems as good as any unless you have some specific reason for not wanting to use it. A second choice would be Guid.Parse("ffffffff-ffff-ffff-ffff-ffffffffffff") but that's a lot uglier IMHO.

    0 讨论(0)
  • 2020-12-16 10:44

    you can make guid variable to accept null first using ? operator then you use Guid.Empty or typecast it to null using (Guid?)null;

    eg:

     Guid? id = Guid.Empty;
    

    or

     Guid? id =  (Guid?)null;
    
    0 讨论(0)
  • 2020-12-16 10:47

    extrac Guid values from database functions:

        #region GUID
    
        public static Guid GGuid(SqlDataReader reader, string field)
        {
            try
            {
                return reader[field] == DBNull.Value ? Guid.Empty : (Guid)reader[field];
            }
            catch { return Guid.Empty; }
        }
    
        public static Guid GGuid(SqlDataReader reader, int ordinal = 0)
        {
            try
            {
                return reader[ordinal] == DBNull.Value ? Guid.Empty : (Guid)reader[ordinal];
            }
            catch { return Guid.Empty; }
        }
    
        public static Guid? NGuid(SqlDataReader reader, string field)
        {
            try
            {
                if (reader[field] == DBNull.Value) return (Guid?)null; else return (Guid)reader[field];
            }
            catch { return (Guid?)null; }
        }
    
        public static Guid? NGuid(SqlDataReader reader, int ordinal = 0)
        {
            try
            {
                if (reader[ordinal] == DBNull.Value) return (Guid?)null; else return (Guid)reader[ordinal];
            }
            catch { return (Guid?)null; }
        }
    
        #endregion
    
    0 讨论(0)
  • 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<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.

    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.

    0 讨论(0)
  • 2020-12-16 10:55

    Since "Guid" is not nullable, use "Guid.Empty" as default value.

    0 讨论(0)
提交回复
热议问题