Just add defaultValue
parameter in CreateTable
method for property:
public partial class TestSimpleEntity : DbMigration { public override void Up() { CreateTable( "dbo.SimpleEntities", c => new { id = c.Long(nullable: false, identity: true), name = c.String(), deleted = c.Boolean(nullable: false, defaultValue: true), }) .PrimaryKey(t => t.id); } public override void Down() { DropTable("dbo.SimpleEntities"); } }
After that, run update-database -verbose
command, you will observe that EF will generate query which will contain Default
value.
Below is the Table Definition from Server Explorer:
CREATE TABLE [dbo].[SimpleEntities] ( [id] BIGINT IDENTITY (1, 1) NOT NULL, [name] NVARCHAR (MAX) NULL, [deleted] BIT DEFAULT ((1)) NOT NULL, CONSTRAINT [PK_dbo.SimpleEntities] PRIMARY KEY CLUSTERED ([id] ASC) );
For the DateTime default value you can use defaultValueSql
:
public partial class MyMigration : DbMigration { public override void Up() { CreateTable("dbo.Users", c => new { Created = c.DateTime(nullable: false, defaultValueSql: "GETDATE()"), }) .PrimaryKey(t => t.ID); ...