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.

Was it helpful?

Solution

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
    }
}

`

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top