Click here to Skip to main content
15,912,756 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
We've developed an ERP application using Visual Studio 2019, C# and ASP.NET MVC. We wish to shift it to a SaaS model.

We used Entity Framework with a data-first approach. And most of our code is using Linq for database access. The SQL Server database connection string is specified in web.config which is created automatically on EF creation.

Sample connection string in web.config:

<add name="myEntities"
="" connectionstring="metadata=res://*/myEntities.csdl|res://*/myEntities.ssdl|res://*/myEntities.msl;provider=System.Data.SqlClient;provider connection string="data source=xx.xx.xx.xxx;initial catalog=XXXcustomerdB;userid=sa;password=xxxpassword;MultipleActiveResultSets=True;App=EntityFramework"" providername="System.Data.EntityClient">


To allow access to a customer we create a website on IIS at a port e.g. 8112. the website is created in a special folder under inetpub (e.g. C:\inetpub\customername).

This customer database (XXXcustomerdB) is created in SQL Server and specified in web.config. This web.config is also copied in the same folder

Now for another customer, the same procedure is followed (a separate port, folder and database).

All the databases created like this have the same database structure and works fine with same EF. So for 10 customers, we'll have 10 folders and 10 different ports

We face the following challenges:

Since we use LINQ, the DbContext is always bound to the connection specified in web.config, which forces us to have separate web.config and hence separate folders for each customer. Also for each customer we've to create and maintain resources (folder, website, IIS port, configuration etc) differently and manually.

Using ADO.NET, we could create a connection string dynamically and access multiple databases based on user's login but LINQ doesn't allow us to do so. We are at a stage where we can't drop LINQ and move to ADO.NET.

We want a solution where we don't have to create separate folder or website or port. The website is hosted simply at root and port 80.

The user login will be authorised from a common database (which may be specified in web.config). This database will have information about the user's database name and other credentials.

This will allow us to implement SaaS infrastucture.

Can anyone please suggest us the solution? Many thanks in advance..

What I have tried:

I looked all over the net but everybody points to multi-tenancy, which is really not the case.
Posted
Updated 28-Jan-20 4:27am

1 solution

When you create your DB Context;

using (db = new MyContext())
{
    var data = db.SomeTable.FirstOrDefault(x => x.ID = 1234);
}


You can pass the connection string as a param

string connName = "Customer-related connection name here";
using (db = new MyContext(connName))
{
    var data = db.SomeTable.FirstOrDefault(x => x.ID = 1234);
}


That will let different users use different databases. Your system is still quite fragile though, it relies on heavy assumptions that all databases are exactly the same.
 
Share this answer
 
Comments
shenghanis@yahoo.com 29-Jan-20 0:46am    
Thanks for the prompt reply, but it doesn't work for me, since MyContext doesn't accept any parameters. I tried to change the MyContext.Context.cs to accept the parameters like,

public MyContext(string connstring="")
: base("name=MyContext")
{
}

but it didn't work
shenghanis@yahoo.com 29-Jan-20 1:18am    
Thanks dear, its solved..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900