0

I work WITH ASP.NET MVC 5 and EF6, I used code-first method to generate a database.

Entity class:

[Table("Simple")]
public class SimpleEntity
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    [Column("id")]
    public long Id { get; set; }

    [Column("name")]
    public string name { get; set; }

    [Column("deleted")]
    public bool deleted { get; set; }

    public SimpleEntity()
    {
    }
}

EntityTypeConfiguration class:

public class SimpleEntityConfig : EntityTypeConfiguration<SimpleEntity>
{
    protected SimpleEntity()
    {
        HasKey(a => a.Id);
    }
}

I want this strategy to generate a table with this query:

CREATE TABLE Simple 
(
    id int NOT NULL,
    name varchar(255) NOT NULL,
    deleted bit DEFAULT 'TRUE'
); 

Important for me to generate a column in the table with DEFAULT value, what is the solution?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Morteza Jalambadani
  • 2,190
  • 6
  • 21
  • 35
  • https://stackoverflow.com/questions/20136504/how-can-set-a-default-value-constraint-with-entity-framework-6-code-first – Rutuja Mar 01 '21 at 11:00

2 Answers2

0
entity.Property(t => t.deleted)
   .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Computed);
Stig
  • 1,974
  • 2
  • 23
  • 50
0

You can create a table with column having default value by simply adding defaultValue in generated migration code.

After creating SimpleEntity Class, run Add-Migration TestSimpleEntity command to generate migration code. You will get below code in Up() method:

 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),
                    })
                .PrimaryKey(t => t.id);
            
        }
        
        public override void Down()
        {
            DropTable("dbo.SimpleEntities");
        }
}

Just add defaultValue parameter in CreateTable method for deleted 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)
);
Dharman
  • 30,962
  • 25
  • 85
  • 135