Developer forum

Forum » Development » DateTime represented as nvarchar(max)

DateTime represented as nvarchar(max)

Niklas Malmqvist
Reply

Hello!

We created a code first item field with date type DateTime and editor DateTimeEditor. The database shows the column data type as nvarchar(max), which seems completely overkill. Is this intended behaviour or did we do something wrong?

Here is the code snippet we used to create the field:

[Group("Project data")]
[Field("LastModified", typeof(Dynamicweb.Content.Items.Editors.DateTimeEditor))]
[Name("LastModified")]
public DateTime? LastModified { get; set; }

The data is imported by an automated process and the users will not be able to modify it, only view it. An alternative would be to make this a string field instead and then string convert the DateTime during the import, e.g. "2019-12-04 11:16:21". Would that be a better option?

Kind regards,

Niklas


Replies

 
Morten Bengtson Dynamicweb Employee
Morten Bengtson
Reply
This post has been marked as an answer

Hi Niklas,

A property of type DateTime will be created as a datetime column in the database.
A property of type DateTime? (Nullable<DateTime>) will be created as an nvarchar(max) column in the database.

This is because the default column type we use is nvarchar(max) and we don't have any specific schema handling for nullable types like DateTime?, int?, double?, bool?, etc.
You could argue that we should have handled nullable types differently, but it's not something that we can easily change now.

So I would suggest that you use a DateTime property instead.
The default value of DateTime is DateTime.MinValue and this will be treated as "Not set" meaning that NULL will be stored in the database.

Best regards,
Morten

Votes for this answer: 1
 
Niklas Malmqvist
Reply

Hi Morten!

Ah I see. Then I will change the type to DateTime instead since it handles null values as well.

Thank you!

Kind regards,
Niklas

 

You must be logged in to post in the forum