Skip to content

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
ASP.Net MVC / ASP.Net WebForms / C# / SQL

How to add default value for Entity Framework migrations for DateTime and Bool

- 19.06.22 - ErcanOPAK

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.

enter image description here

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

 

Related posts:

Replace duplicate spaces with a single space in SQL

Dependency Injection Performance — “AddTransient Everywhere” Is Not the Answer

Rename column SQL Server

Post Views: 772

Post navigation

String Interpolation, String Format, String Concat and String Builder in C#
How to get stored procedure parameters details in SQL

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

December 2025
M T W T F S S
1234567
891011121314
15161718192021
22232425262728
293031  
« Nov    

Most Viewed Posts

  • Get the User Name and Domain Name from an Email Address in SQL (915)
  • Get the First and Last Word from a String or Sentence in SQL (798)
  • How to select distinct rows in a datatable in C# (772)
  • How to add default value for Entity Framework migrations for DateTime and Bool (772)
  • How to make theater mode the default for Youtube (586)
  • Add Constraint to SQL Table to ensure email contains @ (558)
  • How to enable, disable and check if Service Broker is enabled on a database in SQL Server (532)
  • Average of all values in a column that are not zero in SQL (505)
  • Find numbers with more than two decimal places in SQL (421)
  • How to use Map Mode for Vertical Scroll Mode in Visual Studio (416)

Recent Posts

  • VS “Breakpoints Hit Wrong Line” — The PDB Stale Cache Bug
  • WP “Menus Not Saving” — The Max Input Vars Killer
  • Windows 11 “Search Bar Broken or Empty” — Rebuild Index the RIGHT Way
  • Windows 11 “UI Stuttering” — The Hidden Hardware Accelerated GPU Scheduling Bug
  • AJAX “POST Works Locally but Not in Production” — HTTPS Mixed Content
  • JS “setInterval Drift” — Why Your Timers Run Slower Over Time
  • HTML5 “Broken Autofill Styles” — Chrome’s Yellow Background Bug
  • CSS “Z-Index Doesn’t Work!” — The Parent Stacking Context Curse
  • .NET Core “HttpClient Exhaustion” — Why Your API Suddenly Stops Responding
  • ASP.NET Core “Random 404 After Deploy” — Broken Routing Cache

Most Viewed Posts

  • Get the User Name and Domain Name from an Email Address in SQL (915)
  • Get the First and Last Word from a String or Sentence in SQL (798)
  • How to select distinct rows in a datatable in C# (772)
  • How to add default value for Entity Framework migrations for DateTime and Bool (772)
  • How to make theater mode the default for Youtube (586)

Recent Posts

  • VS “Breakpoints Hit Wrong Line” — The PDB Stale Cache Bug
  • WP “Menus Not Saving” — The Max Input Vars Killer
  • Windows 11 “Search Bar Broken or Empty” — Rebuild Index the RIGHT Way
  • Windows 11 “UI Stuttering” — The Hidden Hardware Accelerated GPU Scheduling Bug
  • AJAX “POST Works Locally but Not in Production” — HTTPS Mixed Content

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
© 2025 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com