I'm trying to get my DynamicData Entities to behave like SQL Server when NULL data is entered for a field where I've set the
Default Value or Binding. For example, my [Settings] field is defined as
[Settings] [int] NOT NULL,
but I want it to default to 0:
ALTER TABLE [dbo].[ta_project] ADD CONSTRAINT [DF_ta_project_Settings] DEFAULT ((0)) FOR [Settings]
SQL Server inserts a 0 for Settings, which is what I want. I thought I could accomplish the same thing in my DynamicData Entities using ScaffoldColumn(false). That certainly keeps it out of my UI, but I get a
Cannot insert the value NULL into column 'Settings', table 'ChildCare.dbo.ta_project'; column does not allow nulls. INSERT fails.
error using Insert.aspx
Is there a best practice I should be using to get DynamicData Entities to let the
Default Value or Binding do its job? Thanks in advance for any suggestions.
Hi sjnaughton! I'm building an Dynamid Data entity Site and I have the same problem by setting DefaultValue with one field called "CreationDate"
I try to do this:
[DefaultValue(DateTime.Now)]
and an error appear that An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type
//Then I've tried this second solution without solving my problem:
public DateTime CreationDate
{
get
{
return (this.dateCreated == default(DateTime))
? DateTime.Now
: this.dateCreated;
}
set { this.dateCreated = value; }
}
private DateTime dateCreated = default(DateTime);
//Have you any idea please to resolve this problem ?? thank you
pwmfs33
Member
14 Points
13 Posts
How to get DynamicData Entities to let the (Default Value or Binding) do its job?
Feb 02, 2012 03:38 PM|LINK
Hi,
I'm trying to get my DynamicData Entities to behave like SQL Server when NULL data is entered for a field where I've set the Default Value or Binding. For example, my [Settings] field is defined as
but I want it to default to 0:
If I omit [Settings] from an Insert statement:
SQL Server inserts a 0 for Settings, which is what I want. I thought I could accomplish the same thing in my DynamicData Entities using ScaffoldColumn(false). That certainly keeps it out of my UI, but I get a
error using Insert.aspx
Is there a best practice I should be using to get DynamicData Entities to let the Default Value or Binding do its job? Thanks in advance for any suggestions.
sjnaughton
All-Star
27308 Points
5458 Posts
MVP
Re: How to get DynamicData Entities to let the (Default Value or Binding) do its job?
Feb 03, 2012 10:03 AM|LINK
you will need to set the DefaultValue attribute in your metsdata
[DefaultValue("Test")]Always seeking an elegant solution.
hilquatar
Member
4 Points
2 Posts
Re: How to get DynamicData Entities to let the (Default Value or Binding) do its job?
Mar 22, 2012 02:38 PM|LINK
Hi sjnaughton! I'm building an Dynamid Data entity Site and I have the same problem by setting DefaultValue with one field called "CreationDate"
I try to do this:
[DefaultValue(DateTime.Now)] and an error appear that An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type //Then I've tried this second solution without solving my problem: public DateTime CreationDate { get { return (this.dateCreated == default(DateTime)) ? DateTime.Now : this.dateCreated; } set { this.dateCreated = value; } } private DateTime dateCreated = default(DateTime);sjnaughton
All-Star
27308 Points
5458 Posts
MVP
Re: How to get DynamicData Entities to let the (Default Value or Binding) do its job?
Mar 22, 2012 03:37 PM|LINK
hi hilquatar, you can only have a static value in the DefaultValue Attribute you probable want to do this in the DB with a trigger or you can use my audit sample here Basic Auditing for Dynamic Data with Entity Framework 4.x
Always seeking an elegant solution.
hilquatar
Member
4 Points
2 Posts
Re: How to get DynamicData Entities to let the (Default Value or Binding) do its job?
Mar 26, 2012 12:02 PM|LINK
Yesssss! :) It works very well, thank you very much sjnaughton for helping me! :