[RESOLVED]Administration new customer and create new database in multitenant app

I Need suggestions

I have my clients, and they may have branches

So I filter by CompanyId

For each client I have, it has its own database

How could I create a project management, to create database for new clients and also manage the databases that already exist?

How to create a database after registering a new client?

My English is bad, sorry

I made a simple drawing to try to illustrate what I think

my current architecture

Model – Models

Data – Repositorys, DBContext, Entity Configurations

Utilities – Helpers, Functions

Service – My simple service, not use DI

Web – MY Web App

Hi Rodrigo,

According to your description, I think you want to have a database that used to manage other database.

You could create a new record in the manager database if a new client submit the application, then you could create a new client if this client record in the manager database.

To create a database, you could use EF code first.

There is an article about create database that can help you:

# Database Initialization Strategies in Code-First

http://www.entityframeworktutorial.net/code-first/database-initialization-strategy-in-code-first.aspx  

Best Regards

Starain

 

Starain chen – MSFT

Hi Rodrigo,

According to your description, I think you want to have a database that used to manage other database.

You could create a new record in the manager database if a new client submit the application, then you could create a new client if this client record in the manager database.

To create a database, you could use EF code first.

There is an article about create database that can help you:

# Database Initialization Strategies in Code-First

http://www.entityframeworktutorial.net/code-first/database-initialization-strategy-in-code-first.aspx  

Best Regards

Starain

 

Thanks for answering Storain chen

I think about creating something like this:

1 Create a new project web : Admin.Web

and create new customers

2 º Create new subdomain customer1.mysystem.com customer2.mysystem.com …

3 º In global.asax 

protected void Application_BeginRequest()
{
var subdomain = "App";
var host = HttpContext.Current.Request.Url.Host;
if (host != "localhost")
{
var index = host.IndexOf(".");
subdomain = host.Substring(0, index);
}

var result = App.Init(subdomain);

if (!result)
{
Response.Redirect("URL");
}
}

I’m get subdomain and check in App.Init

    public class App
    {
      public static bool Init(string subdomain)
      {
        var tenant = TenantService.GetTenantBySubdomain(subdomain);
        if (tenant != null)
        {
          
        }
        return false;
      }
    }

if Tenant != null I set database in my connection string:

    public static class ConnString
    {
public static string Database {get;set;} public static string GetConnectionString() { var conn = new SqlConnectionStringBuilder(); conn.DataSource = @"MYSERVER"; conn.InitialCatalog = this.Database; conn.UserID = "user"; conn.Password = "pass@"; return conn.ConnectionString; } }

and in my App Db Context 

  public class MyDbContext : IdentityDbContext<AppUser>
  {
    public MyDbContext()
      : base("AppConnection") //This is ignored
    {
      Database.Connection.ConnectionString = ConnString.GetConnection();
      Database.CreateIfNotExists();

      this.SetCommandTimeOut(300);
    }
}

Then Create if not exist database client

Suggestions?

Is it correct to do so?

What could be improved?

Hi Rodrigo,

In my opinion, this code is ok and I think these database connection string are stored in database.

Best Regards

Starain

Leave a Reply