Entity Framework 6 Code first Default value

marino-krk picture marino-krk · Oct 24, 2013 · Viewed 241.5k times · Source

is there "elegant" way to give specific property a default value ?

Maybe by DataAnnotations, something like :

[DefaultValue("true")]
public bool Active { get; set; }

Thank you.

Answer

gdbdable picture gdbdable · Jan 13, 2015

You can do it by manually edit code first migration:

public override void Up()
{    
   AddColumn("dbo.Events", "Active", c => c.Boolean(nullable: false, defaultValue: true));
}