setting default value for code only RRS feed

  • Question

  • How do u set default value for a property at the database level using Code-Only

    For instance if i have a property called CreateDate and i want its default value in the database to be getdate(), how do i do it. or also another example be select newid() for guids..


    Saturday, May 15, 2010 7:46 PM

All replies

  • Hi Zeeshan,

    Code First doesn't currently provide a mechanism for providing column default values.

    You will need to manually modify the generated DDL to add them.



    Thursday, May 20, 2010 8:34 PM
  • Alternatively, you could probably set the default value in your POCO's constructor:


    public class MyBaseClass
     public MyBaseClass()
    DateCreated = DateTime.Now;
     public Datetime DateCreated { get; set; }


    Sunday, January 2, 2011 8:54 PM