Developer forum

Forum » Development » Is the DefaultValue for DateTime working for code-first items?

Is the DefaultValue for DateTime working for code-first items?

Peter Terkildsen
Reply

I used an approach described here: http://developer.dynamicweb.com/forum/development/default-value-for-itemfield.aspx to do the following code:

        [Field("Active From", typeof(Dynamicweb.Content.Items.Editors.DateTimeEditor))]
        [DefaultValue("NOW")]
        [Required]
        public DateTime Active_From { get; set; }

        [Field("Active To", typeof(Dynamicweb.Content.Items.Editors.DateTimeEditor))]
        [DefaultValue("NOW")]
        [Required]
        public DateTime Active_To { get; set; }

The default value is inserted into the item. However, I receive an SQL-error when I try adding a new item:

http://screencast.com/t/aSgwSdRM6aD

It seems that there's no default value when I try creating a new item.

Regards,

Peter

 


Replies

 
Nicolai Høeg Pedersen
Reply

Have you tried "Now" instead of "NOW"

Have you tried @Code(System.DateTime.Now)?

See this: http://manual.dynamicweb-cms.com/Dynamicweb-On-line-Manual/Items/Item-fields.aspx

 
Peter Terkildsen
Reply

Hi,

I think I've tried everything now :-)

As far as I can see the main issue is that the field is not created as a Datetime field, but instead as an NVARCHAR field. I've tried comparing with the way that the field is created when you create it via the management center.

Peter

 
Peter Terkildsen
Reply

Hi again,

Has anyone succeeded in getting this to work via code-first? I'm stuck right now.

Peter

 
Vladimir
Reply

Hi Peter,

It seems there is a bug with default values for datetime fields - I will register a bug to fix that.

But you may bypass problem by overriding save function (or use constructor to assign defaults)

Best regards,

Vladimir

 
Peter Terkildsen
Reply

Hi Vladimir,

Ok, do you have any idea of when this fix will be made available?

Peter

 
Vladimir
Reply

Hi Peter,

It will in nearest 8.6.1 hotfix

 

Best regards,

Vladimir

 

You must be logged in to post in the forum