Closed JejoSwaks closed 2 months ago
I don't think you're missing anything. Probably just a missed use case. The BulkCopy code has different code paths from sql calls.
Can you make a small PR that adds a test case to the Postgres tests? Then I can just step through and fix it.
You were right - just a null check. I'll tidy up a few other updates then push out a build.
This should be fixed in 8.0.1
Nullable properties in BulkInsert
I am trying to bulk insert a basic POCO that contains some
string
properties. These can, and may beNULL
(null
). However, when the field accessor is being used to write the CSV (using the Postgres driver in my case); I get aNullReferenceException
(on thevalue.ToString()
invocation). I could default all mystring
properties tostring.Empty
but that would be semantically incorrect.I tried to circumvent this by wrapping string inside a
DbNullable<T>
with implicit operators to allow for the logic to properly compare toDbNull
and to allow for implicit conversion (assignment) fromT
.This works for
null
values (since the comparison overloads tell the logic to skip the property/field). For non-null
values, I added aToString()
override to return the wrapped value. This does not work: the accessor that is being generated emits aToString()
callvirt since the database type isDbType.String
. Obviously, the value that is to be compared toDbNull
is now againnull
where I hoped it would be myDbNullable<T>
Either I am an idiot doing something wrong, or, going the wrong way about it. Or, this is a (known) problem (with a known solution). I've read through all the related topics on the wiki pages but couldn't find a solution or workaround.
Basically: I want to have a property that can compare to
DbNull
and also has a customToString()
method.Or - a an additional
null
check here inPostgreSQLInsightDbProvider.cs
: