Skip to content

ErcanOPAK.com

  • ASP.Net WebForms
  • ASP.Net MVC
  • C#
  • SQL
  • MySQL
  • PHP
  • Devexpress
  • Reportviewer
  • About
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:

Filter Rows By Max Date in SQL
Add Constraint to SQL Table to ensure email contains @
How to solve “A cursor with the name already exists” problem?
How to get stored procedure parameters details in SQL
How to hide message window in MS SQL Server
Post Views: 3

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 *

August 2022
M T W T F S S
1234567
891011121314
15161718192021
22232425262728
293031  
« Jul    

Most Viewed Posts

  • Get the First and Last Word from a String or Sentence in SQL (362)
  • How to use Map Mode for Vertical Scroll Mode in Visual Studio (234)
  • Find numbers with more than two decimal places in SQL (203)
  • Confirm before process with ASPxButton in Devexpress (199)
  • ASPxGridView – Disable CheckBox based on condition in GridViewCommandColumn (198)
  • Add Constraint to SQL Table to ensure email contains @ (198)
  • How to solve “Response.Redirect cannot be called in a Page callback” for DevExpress Components (197)
  • How to make some specific word(s) Bold or Underline in ReportViewer (191)
  • Devexpress ASPxGridview Column Grouping in Code (183)
  • Tense Changes When Using Reported Speech (171)

Recent Posts

  • How to create the ShowBlanksValue and ShowNonBlanksValue items in Devex Grid
  • How to get ‘n’th row in Sql Query with OFFSET FETCH NEXT and ROW_NUMBER()
  • How to change the style of scrollbar with CSS
  • How to add scroll bar to div in Asp.Net
  • How to solve “Fatal: Not possible to fast-forward, aborting” problem
  • How to add Output Parameter to SqlDataSource
  • How to get stored procedure parameters details in SQL
  • How to add default value for Entity Framework migrations for DateTime and Bool
  • String Interpolation, String Format, String Concat and String Builder in C#
  • How to get triggers create and update date in SQL

Recent Posts

  • How to create the ShowBlanksValue and ShowNonBlanksValue items in Devex Grid
  • How to get ‘n’th row in Sql Query with OFFSET FETCH NEXT and ROW_NUMBER()
  • How to change the style of scrollbar with CSS
  • How to add scroll bar to div in Asp.Net
  • How to solve “Fatal: Not possible to fast-forward, aborting” problem

Most Viewed Posts

  • Get the First and Last Word from a String or Sentence in SQL (362)
  • How to use Map Mode for Vertical Scroll Mode in Visual Studio (234)
  • Find numbers with more than two decimal places in SQL (203)
  • Confirm before process with ASPxButton in Devexpress (199)
  • ASPxGridView – Disable CheckBox based on condition in GridViewCommandColumn (198)

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter

© 2022 ErcanOPAK.com

Proudly powered by WordPress | Theme: Xblog Plus by wpthemespace.com