Connect to different database based on URL

by user3151766   Last Updated December 04, 2017 10:05 AM

I am developing a SAAS application where : A single web application is hosted in IIS which will serve to multiple companies and each company will have its own database. The web application and databases will be running on the same server.

My question is - what is the best practice to change the database connection string based on the URL and should each function in the service layer pass the dbName/company name to identify the db connection string?

Currently, I am passing the dbname in each function in the data layer and it is returning the connection string. The function then connects to db and does the needful operation.

I have checked several multi tenant db queries online but did not find any answers related to my query. Any example with implementation would be a great help.

The way I do it is as follows :

function Send()
{

    _emailService.SendEmail(emailId, dbName);
}

public class EmailService()
{

    Public SendEmail(string emailId, string dbName)
    {
         var connStr = SQLHelper.GetConnectionStr(dbName);
         ...
         ...
         ...
         connect to db to fetch email details and then send email
    }

}


Related Questions


asp.net ApplicationUser with multi relationship

Updated May 08, 2018 11:05 AM