views:

84

answers:

1

In Entity Framework 4, Code Only (CTP3), how do you set a default value for a property in the POCO's EntityConfiguration class?

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);

     //set default value for CreatedOn ?
 }
}
A: 

Unfortunately you can't. http://social.msdn.microsoft.com/Forums/en/adonetefx/thread/a091ccd6-0ba8-4d4f-8f5e-aafabeb258e4

Yury Tarabanko
Code-first is still a CTP. Please write a Connect report for this, if you haven't already.
Craig Stuntz
Actually I think it was an active design decision. Setting default values in database is a kind of step aside from code only approach.
Yury Tarabanko
Well, I do expect the "Code Only" means everything is configure in the codes. Manually setting the default value in the db isn't a good idea, since I need the entity framework to create the database for me using the context.CreateDatabase() function, and I don't really want to maintain both codes and db script in the same time.
imnd
You can consider default values to be the part of you Person class. When you set default values in the db you'll run into the following problem: those properties won't be initialized until you save your entity. It is a problem in DB First and Model First when you have generated POCOs.
Yury Tarabanko