locked
How to set default values to the metadata? RRS feed

  • Question

  • Hello,

    I have a dataform that is bound to a collection of database entity objects. This entity objects contain a Date field to which I want to default DateTime.Now on creation of a new item. How do I do this?

    At the moment when I create a new object it defaults to DateTime.MinValue.

    Thank you.

    Didier

     

    P.S. There is a hack solution in this forum (http://forums.silverlight.net/forums/p/132085/295080.aspx) but is there a cleaner way of setting default values?

    Wednesday, November 25, 2009 2:31 PM

Answers

  • Hello,

    I found a way to set default values at the DataForm level. I got confused by the DataForm AddingNewItem event because the CurrentItem at this stage is not the new object. There is another event called ContentLoaded at which it is convenient to enter default values. Checking if the mode of the DataForm is "AddNew" is required to avoid having the default values set in Edit and ReadOnly modes.

    The normal way of setting default values should be at the entity class level you would think but I couldn't figure out a way to do so.

    Thanks.

    Didier

    Thursday, November 26, 2009 3:25 PM

All replies

  • I guess you can do different hacks, for example in you Insert operation you will get your entity, and can check if the prop is DataTime.MinValue and then set it to DateTime.Now before it's added to the database. If you use EF 4.0, you can add a private field which you map to the column which as the Date value, then use the .shared feature to add a public property which will check the private property if the value is MinValue, and then return DateTime.Now (ugly solution). The DataForm probably have some event which will be executed when you add new item, for example the AddNewItem or the EditXXX events..
    Wednesday, November 25, 2009 5:58 PM
  • I see.

    So, no clean way of setting default values yet, then?

    I am just thinking of the default values option that you have in SQL when you create a database table. For example, I have the dates set to getDate() in my SQL database table by default and I was wondering if there was a clean way to bring these default values from SQL to the Linq To SQL class or to the metadata class.

     

    Thanks.

     

    Didier

    Wednesday, November 25, 2009 6:27 PM
  • How are you creating the "new" object?

    I haven't tried it but can you not sprinkle the property with data annotation [DefaultValue(DateTime.Now)] ?

    Wednesday, November 25, 2009 6:58 PM
  •  I just tried the following test and I got the below error when compiling:

     

                [Required]
    [Display(Name = "Release Date")]
    [DefaultValue(typeof(DateTime), "2009/11/25")]
    public DateTime ReleaseDate;
      

     Error:

     

    Error	2	The "CreateRiaClientFilesTask" task failed unexpectedly.
    System.NullReferenceException: Object reference not set to an instance of an object.
    at Microsoft.RiaServices.Tools.ClientProxyFixupCodeDomVisitor.GetAttributeId(CodeAttributeDeclaration attribute)
    at Microsoft.RiaServices.Tools.ClientProxyFixupCodeDomVisitor.b__6(CodeAttributeDeclaration a)
    at System.Linq.EnumerableSorter`2.ComputeKeys(TElement[] elements, Int32 count)
    at System.Linq.EnumerableSorter`1.Sort(TElement[] elements, Int32 count)
    at System.Linq.OrderedEnumerable`1.d__0.MoveNext()
    at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
    at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
    at Microsoft.RiaServices.Tools.ClientProxyFixupCodeDomVisitor.VisitCodeAttributeDeclarationCollection(CodeAttributeDeclarationCollection codeAttributeDeclarationCollection)
    at Microsoft.RiaServices.Tools.CodeDomVisitor.VisitCodeMemberProperty(CodeMemberProperty codeMemberProperty)
    at Microsoft.RiaServices.Tools.CodeDomVisitor.VisitCodeTypeMember(CodeTypeMember codeTypeMember)
    at Microsoft.RiaServices.Tools.CodeDomVisitor.VisitCodeTypeMemberCollection(CodeTypeMemberCollection codeTypeMemberCollection)
    at Microsoft.RiaServices.Tools.CodeDomVisitor.VisitCodeTypeDeclaration(CodeTypeDeclaration codeTypeDeclaration)
    at Microsoft.RiaServices.Tools.CodeDomVisitor.VisitCodeTypeDeclarationCollection(CodeTypeDeclarationCollection codeTypeDeclarationCollection)
    at Microsoft.RiaServices.Tools.ClientProxyFixupCodeDomVisitor.VisitCodeTypeDeclarationCollection(CodeTypeDeclarationCollection codeTypeDeclarationCollection)
    at Microsoft.RiaServices.Tools.CodeDomVisitor.VisitCodeNamespace(CodeNamespace codeNamespace)
    at Microsoft.RiaServices.Tools.CodeDomVisitor.VisitCodeNamespaceCollection(CodeNamespaceCollection codeNamespaceCollection)
    at Microsoft.RiaServices.Tools.ClientProxyFixupCodeDomVisitor.VisitCodeNamespaceCollection(CodeNamespaceCollection codeNamespaceCollection)
    at Microsoft.RiaServices.Tools.CodeDomVisitor.VisitBase(CodeCompileUnit codeCompileUnit)
    at Microsoft.RiaServices.Tools.CodeDomVisitor.Visit(CodeCompileUnit codeCompileUnit)
    at Microsoft.RiaServices.Tools.ClientProxyGenerator.FixUpCompileUnit(CodeCompileUnit compileUnit)
    at Microsoft.RiaServices.Tools.ClientProxyGenerator.GenerateProxyClass(String& generatedCode)
    at Microsoft.RiaServices.Tools.CreateRiaClientFilesTask.GenerateClientProxies()
    at Microsoft.RiaServices.Tools.CreateRiaClientFilesTask.ExecuteInternal()
    at Microsoft.RiaServices.Tools.RiaClientFilesTask.Execute()
    at Microsoft.Build.Framework.ITask.Execute()
    at Microsoft.Build.BuildEngine.TaskEngine.ExecuteInstantiatedTask(EngineProxy engineProxy, ItemBucket bucket, TaskExecutionMode howToExecuteTask, ITask task, Boolean& taskResult)
      
    Wednesday, November 25, 2009 7:59 PM
  • [Display(Name = "Release Date")]
    [DefaultValue(typeof(DateTime), "2009/11/25")]
    public DateTime ReleaseDate;

     

    You may have to Parse the Date type like

    [DefaultValue(typeof(DateTime), DateTime.Parse("11/25/2009")]

    or you could do:

    [DefaultValue(typeof(DateTime), DateTime.Now)]

    Try that and see what happens.

    Wednesday, November 25, 2009 8:07 PM
  • [Display(Name = "Release Date")]
    [DefaultValue(typeof(DateTime), "2009/11/25")]
    public DateTime ReleaseDate;

     

    You may have to Parse the Date type like

    [DefaultValue(typeof(DateTime), DateTime.Parse("11/25/2009")]

    or you could do:

    [DefaultValue(typeof(DateTime), DateTime.Now)]

    Try that and see what happens.

     

     The second parameter is supposed to be a string and I tried the following but it doesn't work either because it requires a constant.

     

    [DefaultValue(typeof(DateTime), DateTime.Now.ToString())]

    Thursday, November 26, 2009 12:37 PM

  • I have a dataform that is bound to a collection of database entity objects. This entity objects contain a Date field to which I want to default DateTime.Now on creation of a new item. How do I do this?

    I think the fundamental problem is the fact you can't talk to the server with this modified entity other than submitting changes. There are several scenarios where you want the server to create the new entity and let it populate "context sensitive" default values. Also there are cases when changing a field on the client and tabbing out, you might want to do a server roundtrip to update the entity based on the changed context.

     

    Thursday, November 26, 2009 1:10 PM
  • Hello,

    I found a way to set default values at the DataForm level. I got confused by the DataForm AddingNewItem event because the CurrentItem at this stage is not the new object. There is another event called ContentLoaded at which it is convenient to enter default values. Checking if the mode of the DataForm is "AddNew" is required to avoid having the default values set in Edit and ReadOnly modes.

    The normal way of setting default values should be at the entity class level you would think but I couldn't figure out a way to do so.

    Thanks.

    Didier

    Thursday, November 26, 2009 3:25 PM