title: How to create a column on an existing datatable with a default value for update only
body: <p>I am doing a major refactoring of an existing ASP-Core project. This application is connected to a MS-SQL database using Entity Framework Core, Code First approach.</p>
  <p>I would like to add a non-nullable property to a <code>DbSet</code> which would be defaulted to a certain value when performing the update (to reflect this change on previously inserted entities), but not after the update is performed.</p>
  <p>I managed to update the existing entities by adding a DefaultValue in the <code>migrationBuilder.AddColumn</code> method.</p>
  <p><strong>Problem is : this default value persists</strong>, ie. if I try to add later on a Foo without a LastName, LastName would be defaulted into the DB, which is not what I want (I would like the DB to reject the insertion instead).</p>
  <p>How can I set a <em>temporary default value</em> using Code First Approach?</p>
  <p>The only way I've found so far is to modify the Default Value for this column from MS SQL Server Management Studio after applying this migration, which is not ideal.</p>
  <pre><code>//Existing version
  public class Foo
  {
      [Key]
      public int Id { get; set; }
  
      [MaxLength(240), Required]
      public string FirstName { get; set; }
  
  }
  
  //Updated version
  public class Foo
  {
      [Key]
      public int Id { get; set; }
  
      [MaxLength(240), Required]
      public string FirstName { get; set; }
  
      [MaxLength(240), Required]
      public string LastName { get; set; } //Should not be null
  }
  
  public partial class FooUpdateMigration : Migration
  {
      protected override void Up(MigrationBuilder migrationBuilder) {
          migrationBuilder.AddColumn&lt;string&gt;(
              name: &quot;LastName&quot;,
              table: &quot;Foos&quot;,
              maxLength: 240,
              nullable: false,
              defaultValue: &quot;Unknown for old Foos&quot;);
  
      }
  
      protected override void Down(MigrationBuilder migrationBuilder) {
          migrationBuilder.DropColumn(
              name: &quot;LastName&quot;,
              table: &quot;Foos&quot;);
      }
  }
  
  public class FooManager
  {
  
      public void AddFoo(ApplicationDbContext context)
      {
          Foo newFoo = new Foo(){
              FirstName=&quot;John&quot;,
              LastName = null
          };
  
          context.Foos.Add(newFoo);
          context.SaveChanges(); //DO NOT THROW EXCEPTION, LastName is defaulted to &quot;Unknown for old Foos&quot; into the datable
      }
  }
  </code></pre>