auto-mapping of an entity date field from MINDATE to null

Posts   
 
    
greenstone
User
Posts: 132
Joined: 20-Jun-2007
# Posted on: 15-Nov-2011 20:50:30   

Hi,

Our code uses MINDATE as a standard "null" date, but we need to represent this in the database as database null (main reason is that SQL Server doesn't support date 01-01-0001).

Is there a way to have an entity field always do this mapping (from MINDATE to null and null to MINDATE) for us?

Thanks!

Walaa avatar
Walaa
Support Team
Posts: 14946
Joined: 21-Aug-2005
# Posted on: 16-Nov-2011 09:13:51   

Why are you using MINDATE instead of a nullable dateTime?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39590
Joined: 17-Aug-2003
# Posted on: 16-Nov-2011 14:44:47   

You could use a default constraint in the table (so you can save a null and it will be set to the default 'magic' null date). However magic values are not recommended, use a null value instead.

Frans Bouma | Lead developer LLBLGen Pro
greenstone
User
Posts: 132
Joined: 20-Jun-2007
# Posted on: 16-Nov-2011 17:57:06   

You're right, it would be best to avoid the "magic". The reason we didn't use the nullable date type is that an .xsd is the root definition of our data structure for inport/export from the program. Then we xsd.exe code gen a structure of classes (from this .xsd) to represent this data, deserialize xml into these classes, and finally map this data to the data-layer to be sent to the database.

I just checked, and I believe we should be able to set the "isnillable" on the .xsd elements to have xsd.exe generate them as nullable time. But I'm going to need to look at how an "isnillable" xsd.exe generated nullable type is serialized/deserialized from the xml.

I did find a posting on stackoverflow.com about the "nillable", but sounds like an issue with getting the nullable type to serialized/deserialize as one would hope:

I believe if you add nillable="true" in your XML schema definition, XSD will create a nullable type for that element. This is probably a good idea anyway, since you are implying that this value is indeed nillable and this would make your schema definition more explicit in that regard.

Unfortunately, it still includes the corresponding "Specified" field in this case. If you want the serializer to obey the nullable value directly, you will need to manually remove the "xxSpecified" column from the generated code.

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 18-Nov-2011 05:03:56   

I'm sure that there should be a workaround for your xsd/isnullable situation out there. We can't help you with that though.

Another thing you could do is use a TypeConverter that converts from/to a null datetime in DB from/to a MinValue. There are plenty of information about TypeConverters in this forum and also you could grab the sourcecode of the built-in ones from the LLBLGen customer area.

David Elizondo | LLBLGen Support Team