C# – EntityFramework Code First FluentAPI DefaultValue in EF6.X

cef-code-firstef-fluent-apientity-framework

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

Best Answer

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