1

I stumbled upon a entity framework related question which I havn't found any answer for yet. When I try to add or alter a column while using code first migrations and set a default value for this column, I have the possibility to use either the defaultValue and/or the defaultValueSql argument.

The official MSDN documentation writes following:

ColumnModel.DefaultValue:

Gets or sets a constant value to use as the default value for this column.

ColumnModel.DefaultValueSql:

Gets or sets a SQL expression used as the default value for this column.

Since different default value related questions on stackoverflow use different approaches, can someone please tell me when to use one or another?

For my specific use-case, I want to set a constant value of 4 for my database column, like this:

public override void Up()
{
    this.AddColumn("dbo.Foo", "Bar", c => c.Int(nullable: false, defaultValue: 4));
}

Which results in the following:

SQL Server Management Studio Table Designer

Although this looks absolutely right, all the values of the data rows are set to zero... 🤔 Does anyone has any idea, why?

EDIT: Since @Vidmantas Blazevicius stated that default values will not be automatically applied, I tried to add following code to my Configuration class:

internal sealed class MigrationConfiguration : DbMigrationsConfiguration<DatabaseContext>
{
    protected override void Seed(DatabaseContext context)
    {
        context.Database.ExecuteSqlCommand("UPDATE [dbo].[Foo] SET [Bar] = 4");
    }
}

But this also does not work. Do you guys have any idea how I set the default value after adding a new column?

7
  • it doesn't retrospectively add the default values, try inserting new record Commented Apr 23, 2018 at 9:02
  • @VidmantasBlazevicius I have edited my question Commented Apr 23, 2018 at 9:26
  • Again - I don't think Seed is executed upon migration step - it is only when you create new database. You can add migration with Add-Migration RunSqlScript and then add your custom sql to update something. Commented Apr 23, 2018 at 9:32
  • @VidmantasBlazevicius The strange thing is, that the seed method gets called every time the application starts :-/ I give your idea a try, thanks in advance! Commented Apr 23, 2018 at 9:42
  • 1
    It appears we were both right - there are two Seed methods - one gets called every time, another gets called on db initialization only. I would still go with the Add-Migration for your needs. (stackoverflow.com/questions/24142107/…) Commented Apr 23, 2018 at 9:53

1 Answer 1

1

The default values aren't retrospectively added for existing records when adding new column to an existing table.

You can use Add-Migration RunSqlScript to add a new migration step and update the values in the new column with custom Sql.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.