2

As in title, I have a code, where I recieve DbNull type with null value, when there is no geometry in database and DbGeometry get null value. But when I want to make an if statement to check if DbGeometry equals to DbNull.Value I can't do that. It's something like this:

var geom = entity.Select(entity => entity.Geom);
if (geom == DbNull.Value) return null; // Here it is not valid for VisualStudio

But when i do something like that:

if (geom == null) return null;

It works just fine. Why I get an error when I try to compare geom variable which is a DbGeometry type with DbNull.Value even though without handling a null I get a DbNull type.

MichalT
  • 91
  • 4

1 Answers1

1

DBNull is used by COM interop to distinguish between a VT_NULL variant, which indicates a nonexistent value, and a VT_EMPTY variant.

Do not confuse the notion of null in an object-oriented programming language with a DBNull object. In an object-oriented programming language, null means the absence of a reference to an object. DBNull represents an uninitialized variant or nonexistent database column.

"Remarks The DBNull class represents a nonexistent value. In a database, for example, a column in a row of a table might not contain any data whatsoever. That is, the column is considered to not exist at all instead of merely not having a value. A DBNull object represents the nonexistent column. Additionally, COM interop uses the DBNull class to distinguish between a VT_NULL variant, which indicates a nonexistent value, and a VT_EMPTY variant, which indicates an unspecified value."

https://msdn.microsoft.com/en-us/library/system.dbnull(v=vs.110).aspx

In EF you don't have to use DBNull, you just define nullable property and if you have a reference type then just set the related Id to nullable!

More Info:

What is the point of DBNull?

Community
  • 1
  • 1
Bassam Alugili
  • 16,345
  • 7
  • 52
  • 70