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:
Gets or sets a constant value to use as the default value for this column.
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:
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?

Add-Migration RunSqlScriptand then add your custom sql to update something.Seedmethods - one gets called every time, another gets called on db initialization only. I would still go with theAdd-Migrationfor your needs. (stackoverflow.com/questions/24142107/…)