EntityFramework Code First FluentAPI DefaultValue in EF6.X

Tony Bao picture Tony Bao · Nov 25, 2013 · Viewed 27.7k times · Source

How can I set the default value using EntityFramework Code First FluentAPI for bool property?

Something like:

Property(l => l.PropertyFlag).HasColumnType("bit").DefaultValue(1);

Answer

htxryan picture htxryan · Feb 11, 2014

Good news, code first now supports this. In the "Up()" method of the generated migration, specify a default with the following syntax:

AddColumn("[table name]", "[column name]", c => c.Boolean(nullable: false, defaultValue: false));

MSDN for "AddColumn" method