jqBootstrapValidation adds ajax validation support

July 23, 2012 at 8:23 AMPrescott

jqBootstrapValidation - an excellent validation framework build on top of jquery (for use with twitter bootstrap forms), has just released v1.3.1. I specifically requested an ajax style validator, but David wasn't sure he had the time to add it. I cloned his repo, fully intending to build the ajax support in, however life got in the way and David beat me to it.

Check it out, jqBootstrapValidation sniffs html validation attributes and add additional validation options via the very unobtrusive data attributes tags on your fields.

Posted in: Open Source

Tags: ,

Entity Framework Code First & Default Column Values

May 16, 2012 at 2:41 PMPrescott

One of the issues with code first when creating a database is that you are a bit limited in what code first can do when creating a database, specifically in my case, I couldn’t set a default value.

There are really two different ways to go about accomplishing a default value within code

1. Setting the default in the entity constructor:

 1: public class User
 2: {
 3:  
 4:     public User()
 5:     {
 6:         this.CreatedDate = DateTime.Now;
 7:     }
 8:  
 9:     [DatabaseGenerated(DatabaseGeneratedOption.Identity)] 
 10:     public DateTime CreatedDate { get; set; };
 11: }

This works pretty well, you’ll want to make sure you add the [DatabaseGenerated(DatabaseGeneratedOption.Identity)] attribute, this ensures that CreatedDate only gets saved to the database when you are adding a row, not when you are saving. So the CreatedDate will only be set once, any other time you modify the record, if you change the CreatedDate, the value will not be persisted.

2. Hooking into the Database.SetInitializer to run custom database queries:

Global.asax.cs

 1: protected void Application_Start()
 2: {
 3:     Database.SetInitializer(new MyDatabaseInitializer());
 4: }

MyDatabaseInitializer.cs

 1: public class MyDatabaseInitializer : DropCreateDatabaseIfModelChanges<DbContext>
 2:   {
 3:       protected override void Seed(DbContext context)
 4:       {

5: context.Database.ExecuteSqlCommand(string.Format("ALTER TABLE {0} ADD DEFAULT ({1}) FOR [{2}]",

"Users", "getdate()", "CreatedDate"));

 6:       }
 7:   }

Next time, I’ll look at creating custom DataAnnotations to make setting default values seem more like native code first model development.

Posted in: C# | Code First | Entity Framework

Tags: