NOTE: Defaults are currently only supported under SQL CE implementations. If you need it for another store, let me know

Defaults are, unfortunately, not 100% intuitive due to limitations on how .NET Attributes work.

For standard numeric or string default values, they are pretty straightforward, just define your Field using the DefaultValue attribute parameter, like the one used in Title below:

    [Entity(KeyScheme.Identity)]
    public class Book
    {
        [Field(IsPrimaryKey=true)]
        public int BookID { get; set; }

        [Field(DefaultValue="[untitled]")]
        public string Title { get; set; }
    }

If, however, you with to use the current date (i.e. GetDate in SQL), it's not quite the same because we can't pass a class instance into the Attribute. Instead, we use the DefaultType attribute and use the DefaultType.CurrentDateTime constant. This will inject the GetDate() into the table creation. This behavior can be overridden as well, but the following should work for nearly all cases:

    [Entity(KeyScheme.Identity)]
    public class Book
    {
        [Field(IsPrimaryKey=true)]
        public int BookID { get; set; }

        [Field(DefaultType = DefaultType.CurrentDateTime)]
        public DateTime CreateDate { get; set; }
    }

Last edited Feb 4, 2013 at 4:23 PM by ctacke, version 1

Comments

No comments yet.