I am exposing my repository operations through web api. Repository has been implemented with Entity framework and Unit Of Work Pattern. I have many instances of the same database. Each one represent the data of a different Client. Now the issue is
how can I set the connection string dynamically through each webapi call? Should I get connection string parameter with each call ? Or I should host web Api per client ?
1) Assign a unique id to each client.
2) Store each connection string corresponding to each db instance in Web.Config
3) Accept client id as one of the parameter of your rest Api service.
4) Then read corresponding connection string from Web.Config based on client id parameter of your rest service
You have many choices, the simplest one is to create different Ado.Net Entity Data Model for each instance of the database. Another approach is to change the DbContext connection string at run time. Create different connection strings in the Web.config file.
For example, we got two connection strings in Web.config file:
I add Ado.Net Entity Data Model for Database1, it automatically generate connect string Database1Entities for me, Database2Entities is just a copy of Database1Entities. Now we only have one EDMX in our project, we can change this EDMX connection string to
Database2Enties with the following code:
//Change connection string from Database1Entities to Database2Entities
Database1Entities db = new Database1Entities();
var efConnStrBuilder = new EntityConnectionStringBuilder
(ConfigurationManager.ConnectionStrings["Database2Entities"].ConnectionString);
var sqlConnStrBuilder = new SqlConnectionStringBuilder
(efConnStrBuilder.ProviderConnectionString);
db.Database.Connection.ConnectionString = sqlConnStrBuilder.ConnectionString;
connectionstringwebapiEntityFramework6
We are trying to better understand customer views on social support experience. Click HERE to participate the survey. Thanks!
Member
6 Points
24 Posts
Dynamic Connection String with Web Api
Jun 17, 2015 02:21 PM|internetbeans|LINK
I am exposing my repository operations through web api. Repository has been implemented with Entity framework and Unit Of Work Pattern. I have many instances of the same database. Each one represent the data of a different Client. Now the issue is how can I set the connection string dynamically through each webapi call? Should I get connection string parameter with each call ? Or I should host web Api per client ?
connectionstring webapi EntityFramework6
Contributor
2290 Points
493 Posts
Re: Dynamic Connection String with Web Api
Jun 17, 2015 07:59 PM|santhoshje|LINK
Please try like this
1) Assign a unique id to each client.
2) Store each connection string corresponding to each db instance in Web.Config
3) Accept client id as one of the parameter of your rest Api service.
4) Then read corresponding connection string from Web.Config based on client id parameter of your rest service
connectionstring webapi EntityFramework6
Participant
893 Points
137 Posts
Re: Dynamic Connection String with Web Api
Jun 19, 2015 05:46 AM|Caillen Zhong|LINK
Hello,
You have many choices, the simplest one is to create different Ado.Net Entity Data Model for each instance of the database. Another approach is to change the DbContext connection string at run time. Create different connection strings in the Web.config file.
For example, we got two connection strings in Web.config file:
I add Ado.Net Entity Data Model for Database1, it automatically generate connect string Database1Entities for me, Database2Entities is just a copy of Database1Entities. Now we only have one EDMX in our project, we can change this EDMX connection string to Database2Enties with the following code:
connectionstring webapi EntityFramework6