public IActionResult RegisterDb([FromQuery] string tenant) // If we talk about db or schema strategies this method's job will be better to delegate to a separate worker or service. // And notify your tenant later that he can start work. App has to create db or schema and it can be time consumable. { if (string.IsNullOrWhiteSpace(tenant)) { return(BadRequest("Tenant name is required!")); } var reg = new Regex("[0-9a-zA-Z$_]+"); if (!reg.IsMatch(tenant)) { return(BadRequest("Tenant name must follow this pttern [0-9a-zA-Z$_]+")); } var newTenant = new Models.Tenant { ConStr = $"{_configuration.GetConnectionString("db")}_{tenant}", Key = tenant, Name = tenant }; var connectionString = _configuration.GetConnectionString("db"); var optionsBuilder = new DbContextOptionsBuilder <DbBasedContext>() .UseLazyLoadingProxies() .UseNpgsql(newTenant.ConStr); using (var db = new DbBasedContext(optionsBuilder.Options)) { db.Database.Migrate(); } Services.TenantResolver.Tenants.Add(newTenant);// add new tenant to collection of tenants return(Ok("Tenant was created")); }
public DbBlogPostsController(DbBasedContext db) { _db = db; }