If you are starting with a fresh schema then it is possible to intercept the schema creation and alter the identity creation. If the schema is already created (I.e. counting on migrations) not so. SQL for instance will allow for reseeding an identity, but not changing the increment.
This example uses a table called [Somethings] and checks for when the table is created, substituting the "IDENTITY" with IDENTITY(100000,100000) if found. Unfortunately to my knowledge with EF 6 the interceptor can't be attached/detached from the context, so you can use a flag to ensure the interceptor check/replace code doesn't run all the time by default.
To intercept the schema creation:
public class Interceptor : IDbCommandInterceptor
{
void IDbCommandInterceptor.NonQueryExecuted(DbCommand command, DbCommandInterceptionContext<int> interceptionContext)
{ }
void IDbCommandInterceptor.NonQueryExecuting(DbCommand command, DbCommandInterceptionContext<int> interceptionContext)
{
if (interceptionContext.DbContexts.OfType<SomethingDbContext>().Any(x => !x.ShouldIntercept))
return;
var regex = "NOT NULL IDENTITY,";
var replacement = "NOT NULL IDENTITY(100000,100000),";
if (command.CommandText.StartsWith("CREATE TABLE [dbo].[Somethings]"))
command.CommandText = Regex.Replace(command.CommandText, regex, replacement);
}
void IDbCommandInterceptor.ReaderExecuted(DbCommand command, DbCommandInterceptionContext<DbDataReader> interceptionContext)
{ }
void IDbCommandInterceptor.ReaderExecuting(DbCommand command, DbCommandInterceptionContext<DbDataReader> interceptionContext)
{ }
void IDbCommandInterceptor.ScalarExecuted(DbCommand command, DbCommandInterceptionContext<object> interceptionContext)
{ }
void IDbCommandInterceptor.ScalarExecuting(DbCommand command, DbCommandInterceptionContext<object> interceptionContext)
{ }
}
public class SomethingConfiguration : DbConfiguration
{
public SomethingConfiguration()
{
AddInterceptor(new Interceptor());
}
}
Then in your DbContext:
[DbConfigurationType(typeof(SomethingConfiguration))]
public class SomethingDbContext : DbContext
{
public bool ShouldIntercept { get; set;} = false;
// .. DbSets, etc.
}
By default your DbContext will not actively intercept the commands commands since we really only want to activate this overhead check once. When your application starts you can initialize a DbContext, set the ShouldIntercept to True, then execute a simple query which will ensure the DbContext schema inspection/creation runs:
using (var context = new SomethingDbContext())
{
context.ShouldIntercept = true;
var test = context.Somethings.Any(); // Triggers schema check/creation.
}