Nullable GUID

后端 未结 10 2109
北荒
北荒 2020-12-15 04:36

In my database, in one of the table I have a GUID column with allow nulls. I have a method with a Guid? parameter that inserts a new data row in the table. However when I s

10条回答
  •  醉梦人生
    2020-12-15 04:45

    The ADO.NET API has some problems when it comes to handling nullable value types (i.e. it simply doesn't work correctly). We've had no end of issues with it, and so have arrived at the conclusion that it's best to manually set the value to null, e.g.

    myNewRow.myGuidColumn = myGuid == null ? (object)DBNull.Value : myGuid.Value
    

    It's painful extra work that ADO.NET should handle, but it doesn't seem to do so reliably (even in 3.5 SP1). This at least works correctly.

    We've also seen issues with passing nullable value types to SqlParameters where the generated SQL includes the keyword DEFAULT instead of NULL for the value so I'd recommend the same approach when building parameters.

提交回复
热议问题