title: How to switch between DatabaseGeneratedOption.Identity, Computed and None at runtime without having to generate empty DbMigrations
body: <p>I am migrating a legacy database to a new database which we need to access and "manage" (as oxymoronic as it might sound) primarily through Entity Framework Code-First.</p>
  
  <p>We are using MS SQL Server 2014.</p>
  
  <ol>
  <li><p>The legacy database contained some tables with <strong>computed</strong> columns. <strong>Typical GUID and DateTime stuff.</strong></p></li>
  <li><p>Technically speaking, these columns did not have a computed column specification, but rather where given a default value with <code>NEWID()</code> and <code>GETDATE()</code></p></li>
  </ol>
  
  <p>We all know that it is very easy to configure the <code>DbContext</code> to deal with those properties as follows:</p>
  
  <pre><code>modelBuilder.Entity&lt;Foo&gt;()
              .Property(t =&gt; t.Guid)
              .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Computed);
  modelBuilder.Entity&lt;Bar&gt;()
              .Property(t =&gt; t.DTS)
              .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Computed);
  </code></pre>
  
  <p>The above would instruct the Entity Framework to ignore submitting any supplied values for such properties during <code>INSERTs</code> and <code>UPDATEs</code>.</p>
  
  <ol>
  <li><p>But now <strong>we need to allow for import of legacy records and maintain the OLD values</strong>, including the <strong>PRIMARY KEY</strong>, which is marked as <code>IDENTITY</code> </p>
  
  <ol>
  <li><p>This means we would have to set the <code>Id</code>, <code>Guid</code> and <code>DTS</code> properties to <code>DatabaseGeneratedOption.None</code> while inserting those records.</p></li>
  <li><p>For the case of <code>Id</code>, we would have to somehow execute <code>SET IDENTITY_INSERT ... ON/OFF</code> within the connection session.</p></li>
  <li><p><strong>And we want to do this 
  importing process via Code-First as well.</strong></p></li>
  </ol></li>
  <li><p>If I modify the model and "temporarily" and set those properties to <code>DatabaseGeneratedOption.None</code> after the database has been created, we would get the typical:</p>
  
  <p><em>The model backing the context has changed since the database was created. Consider using Code First Migrations to update the database</em>.</p></li>
  <li><p>I understand that we could generate an empty coded-migration with <code>-IgnoreChanges</code> so as to "establish" this latest version of the context, but this wouldn't be an acceptable strategy as we would have to be run empty migrations back-and-forth solely for this purpose.</p></li>
  </ol>
  
  <hr>
  
  <h3>Half an answer:</h3>
  
  <p>We have considered giving these properties nullable types, i.e.</p>
  
  <pre><code>public class Foo
  {
      ...
      public Guid? Guid { get; set; }
  }
  
  public class Bar
  {
      ...
      public DateTime? DTS { get; set; }
  }
  </code></pre>
  
  <p>While caring about the default values in an initial <code>DbMigration</code>:</p>
  
  <pre><code>CreateTable(
      "dbo.Foos",
      c =&gt; new
          {
              Id = c.Int(nullable: false, identity: true),
              Guid = c.Guid(nullable: false, defaultValueSql: "NEWID()"),
          })
      .PrimaryKey(t =&gt; t.Id);
  
  
  CreateTable(
      "dbo.Bars",
      c =&gt; new
          {
              Id = c.Int(nullable: false, identity: true),
              DTS = c.Guid(nullable: false, defaultValueSql: "GETDATE()"),
          })
      .PrimaryKey(t =&gt; t.Id);
  </code></pre>
  
  <hr>
  
  <h2>The Question:</h2>
  
  <p>But the question remains: Is there a way to switch between <code>DatabaseGeneratedOption.Identity</code>, <code>DatabaseGeneratedOption.Computed</code> and <code>DatabaseGeneratedOption.None</code> at runtime?</p>
  
  <p>At the very least, how could we turn <code>DatabaseGeneratedOption.Identity</code> on/off at runtime?</p>