Skip to content

Instantly share code, notes, and snippets.

@bhushang19
Created March 3, 2019 03:06
Sample DbContextFactory
public class DbContextFactory : IDesignTimeDbContextFactory<YourDbContext>
{
public DbContextFactory()
{
}
public YourDbContext CreateDbContext(string[] args)
{
throw new NotImplementedException();
}
public async Task<YourDbContext> CreateDbContext(IConfiguration configuration, UserProfile userProfile)
{
// Initiallize the tenant provider
ITenantProvider tenantProvider = new ConfigurationTenantProvider(configuration, userProfile);
string connectionString = await tenantProvider.GetTenant();
return await Create(connectionString);
}
public async Task<YourDbContext> Create(string connectionString)
{
var optionsBuilder = new DbContextOptionsBuilder<YourDbContext>();
// Configure Sql server connection
optionsBuilder.UseSqlServer(connectionString, sqlOptions =>
{
sqlOptions.EnableRetryOnFailure(maxRetryCount: 10,
maxRetryDelay: TimeSpan.FromSeconds(30),
errorNumbersToAdd: null);
});
YourDbContext context = new YourDbContext(optionsBuilder.Options);
return await Task.FromResult<YourDbContext>(context);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment