In Entity Framework Code First approach, how do you set a default value for a property in the POCO\'s EntityConfiguration class?
public class Person
{
pu
I'm not sure as of what version of EF this becomes available, but as of version 5 for sure you can handle this. Just set a default value of GetDate() on the column in SQL, either in TSQL or the designer, then setup your EF classes as follows:
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
public DateTime CreatedOn { get; set; }
}
public class PersonConfiguration : EntityConfiguration<Person>
{
public PersonConfiguration()
{
Property(p => p.Id).IsIdentity();
Property(p => p.Name).HasMaxLength(100);
this.Property(p => p.CreatedOn ).HasDatabaseGeneratedOption(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Computed);
}
}
Note that you don't need to make the database column nullable, or the POCO property.