Using Constraint with custom data table - Cannot insert nulls
Hi,
I have a DTO where one of the properties has a Constraint of
[Column("DateAdded")]
[Constraint(Default = "getdate()")]
public DateTime DateAdded { get; set;
When I try to insert a new record, I receive the exception:
Cannot insert the value NULL into column 'DateAdded', table 'bumbraco.dbo.Comment'; column does not allow nulls. INSERT fails.
The statement has been terminated.
Shouldn't the constraint automatically add "getdate()" for each newly record added?
By the way, on SQL Management Studio, I was able to see that "Default Value or Binding" for the DateAdded column is actually "getdate()" but for some reason, it is not generating a new Date with every new entry.
I will stick to pre-populating the field myself instead of relying on that.
Using Constraint with custom data table - Cannot insert nulls
Hi, I have a DTO where one of the properties has a Constraint of
When I try to insert a new record, I receive the exception:
Shouldn't the constraint automatically add "getdate()" for each newly record added?
Thanks /Bilal
Hi Bilal,
you can add an attribute of type
[NullSetting(NullSetting = NullSettings.Null)]
which is used to allow nulls to your column.Hope this helps!
/Michaël
This eliminates the exception, but still the "getdate()" is not taking effect. A null is now inserted.
Does this Constraint Data Annotation work?
Thanks
Hi Bilal,
The constraint is a string.
Why not create a default constructor in your DTO where you assign the current date and time to
DateAdded
?/Michaël
By the way, on SQL Management Studio, I was able to see that "Default Value or Binding" for the DateAdded column is actually "getdate()" but for some reason, it is not generating a new Date with every new entry.
I will stick to pre-populating the field myself instead of relying on that.
Thanks as usual Michael.
/Bilal
No problem Bilal, glad I could help you out!
Have a nice day
/Michaël
is working on a reply...
This forum is in read-only mode while we transition to the new forum.
You can continue this topic on the new forum by tapping the "Continue discussion" link below.