title: EF Core 2 Provider specific HasDefaultValueSql
body: <p>I am trying to use my <code>DbContext</code> with multiple providers. So far everything works okay, except when using <code>HasDefaultValueSql()</code>.  </p>
  
  <p>What I am trying to do is to have a property <code>AuditTimestamp</code> that has the current time when added (and saved) on the <code>dbContext</code>. This can easily be done using something like this that works for the SQL Server provider</p>
  
  <pre><code>modelBuilder
     .Entity(entityType.ClrType)
     .Property(nameof(AuditBase.AuditTimestamp))
     .HasDefaultValueSql("SYSUTCDATETIME()");
  </code></pre>
  
  <p>But when trying to use the same <code>dbContext</code> with SqLite provider, it doesn't work since <code>SYSUTCDATETIME</code> isn't a valid SQL function in SqLite. </p>
  
  <p>Based on <a href="https://stackoverflow.com/questions/46088952/ef-core-2-0-provider-specific-model-configuration">this question</a> I tried to do</p>
  
  <pre><code>modelBuilder
     .Entity(entityType.ClrType)
     .Property(nameof(AuditBase.AuditTimestamp))
     .HasDefaultValueSql(Database.IsSqlite() ? "DATETIME('now')" : "SYSUTCDATETIME()");
  </code></pre>
  
  <p>But it generates this in the migrations</p>
  
  <pre><code>b.Property&lt;DateTime&gt;("AuditTimestamp")
     .ValueGeneratedOnAdd()
     .HasDefaultValueSql("SYSUTCDATETIME()");
  </code></pre>
  
  <p>because I use a SQL Server connection string when generating the migrations. This implies that I need different sets of migrations based on what provider I use which shouldn't be the case. </p>
  
  <p>Based on <a href="https://stackoverflow.com/questions/52365060/entity-framework-core-2-1-multiple-providers">this question</a> it is possible to have provider specific configurations in the migration files but the example is based on auto generated Id column and I don't know how to apply this to <code>HasDefaultValueSql()</code></p>
  
  <p>From <a href="https://github.com/aspnet/EntityFrameworkCore/issues/10193" rel="nofollow noreferrer">here</a> I found that this might work</p>
  
  <pre><code>HasDefaultValueSql("CURRENT_TIMESTAMP")
  </code></pre>
  
  <p>But I would like a more general solution for when you have two (or more) providers that don't share same SQL function names. </p>
  
  <p>The providers I use is SQL Server for production and SqLite for unit testing. EF Core is version 2.2</p>