2

I'm using the code first pattern with Entity Framework 4.0. I want some columns in the DB to have default values.

public class Customer
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }        
    //I need some kind of attribute like this
    [ColumnAttribute.Default = false]
    public bool IsEnabled { get; set; }
}

I'm looking for a way to define the IsEnabled column default value to be false.

steenslag
  • 79,051
  • 16
  • 138
  • 171
RonyK
  • 2,644
  • 5
  • 32
  • 42

2 Answers2

1

I'm not aware of any data annotations that can achieve what you're going for. I think just going old school and setting it in your constructor is the way to go.

Giovanni B
  • 1,032
  • 1
  • 9
  • 12
  • 1
    The problem is that I want it to be a property of the DB generated by the entity framework, so it's not exactly what I need. – RonyK Jul 24 '12 at 13:17
  • I see what you're saying but that's definitely breaking the code first paradigm. The whole concept of code first is to essentially not touch the database and allow EF to set up and manage everything for you. Code first is certainly not for everyone and every use case, but if you decide to go code first, you should stick to the basic concepts. – Giovanni B Jul 24 '12 at 13:24
1

There is no annotation for default values but you can set the default values in the constructor of your POCO.

Mher Sarkissian
  • 131
  • 2
  • 5
  • 1
    The problem is that I want it to be a property of the DB generated by the entity framework. – RonyK Jul 24 '12 at 07:55