locked
How to get DynamicData Entities to let the (Default Value or Binding) do its job? RRS feed

  • Question

  • User-434155841 posted

    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

    	[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]

    If I omit [Settings] from an Insert statement:

    INSERT INTO [project] ([ProjectType] ,[ParticipationType] ,[Status] ,[Name] ,[Phase]]) VALUES (1, 1 , ,1 ,'newname', 1)

    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.

    Thursday, February 2, 2012 11:38 AM

Answers

  • User-330204900 posted

    you will need to set the DefaultValue attribute in your metsdata

    [DefaultValue("Test")]
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 3, 2012 6:03 AM
  • User-330204900 posted

    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

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, March 22, 2012 11:37 AM

All replies

  • User-330204900 posted

    you will need to set the DefaultValue attribute in your metsdata

    [DefaultValue("Test")]
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 3, 2012 6:03 AM
  • User1688382757 posted

    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
    Thursday, March 22, 2012 10:38 AM
  • User-330204900 posted

    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

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, March 22, 2012 11:37 AM
  • User1688382757 posted

    Yesssss! :) It works very well, thank you very much  sjnaughton for helping me! :Cool

    Monday, March 26, 2012 8:02 AM