/DbContextFactory.cs Secret
Created
March 3, 2019 03:06
Sample DbContextFactory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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