Using Entity Framework 6 Code First can I specify a default value for a Boolean field?

ef-migrations entity-framework-6

Question

I am using EF6 and trying to use Code First with Migrations against a SQL DB.

Is it possible using a data annotation in my POCO class to specify that the default value of a Boolean or Bit field should be true?

I know I could modify the data migrations code to add it to the specific migration class but would like to avoid that.

1
2
2/10/2014 9:06:30 PM

Accepted Answer

check this: How to set default value for POCO's in EF CF?

You can do this through Migration step.

public class Test
{
(...)

public bool TestProperty { get; set; }

(...)
}



public partial class AddTestClass : DbMigration
{
    public override void Up()
    {
        CreateTable(
            "Test",
            c => new
                {
                    (...)
                    TestProperty = c => c.Boolean(nullable: false, defaultValue: false)
                    (...)
                })
            (...)
    }

    public overide void Down()
    {
        // Commands for when Migration is brought down
    }
}

`

2
5/23/2017 12:15:07 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow