We want to develop a multi-tenat application using EF code first model, where each tenant will have their own separate database. Since, we are expecting bit high-number of tenants, we want to maintain the connection pool across tenants.
In order to maintain the connection pool, we have planned as given below:
1. Connect to AppMaster (that application master database)
2. Change the database to respective tenant database
3. Handover the control to EF
In order to achieve this, we have created new connection class (called XDConnection) that derived from DbConnection. We have also added extra element in the connection string that contains tenant database name.
Our connection string will be:
```
Server=dbserver;Database=AppMaster;Integrated Security=True;TenantDb=tenant1Db
```
XDConnection class is just the wrapper on top of SQL Connection, the class will be responsible for:
1. Parse the connection string, that separates actual SQL connection string and tenant database name
2. Pass the SQL Connection string to SQL Connection
3. On open, open the SQL connection that open the connection and connect to the AppMaster database
4. Change the database to tenant database
Now, to use this connection, we have also created customized connection factory (i.e. XDConnection factory) that provides object of XDConnection, DependencyResolver (i.e. XDDependency Resolver) that provides the connection factory. And finally injected the XDDependecy resolver through config.
For the PoC purpose, we have just created sample wrapper connection that just map all the required properties/methods to underneath SQL connection respective properties/methods:
Example: In XDConnection:
```
public override string Database
{
// _baseConnection is the object SQL Connection
get { return _baseConnection.Database; }
}
```
After all these:
1. I am not able to create the database using the customized connection class
2. I can create a database using in-built SQL Connection. But, I am still not able to use the customized connection class, it throws error while saving the data
I have posted complete sour code and exception [here](https://entityframework.codeplex.com/discussions/442427)
Please provide me the solution so that I can use EF in my project.
Regards,
Hitesh
Comments: Thanks RoMiller for your quick response. This also looks like a bug to me, it would be great if team can take it up along with other bugs...
In order to maintain the connection pool, we have planned as given below:
1. Connect to AppMaster (that application master database)
2. Change the database to respective tenant database
3. Handover the control to EF
In order to achieve this, we have created new connection class (called XDConnection) that derived from DbConnection. We have also added extra element in the connection string that contains tenant database name.
Our connection string will be:
```
Server=dbserver;Database=AppMaster;Integrated Security=True;TenantDb=tenant1Db
```
XDConnection class is just the wrapper on top of SQL Connection, the class will be responsible for:
1. Parse the connection string, that separates actual SQL connection string and tenant database name
2. Pass the SQL Connection string to SQL Connection
3. On open, open the SQL connection that open the connection and connect to the AppMaster database
4. Change the database to tenant database
Now, to use this connection, we have also created customized connection factory (i.e. XDConnection factory) that provides object of XDConnection, DependencyResolver (i.e. XDDependency Resolver) that provides the connection factory. And finally injected the XDDependecy resolver through config.
For the PoC purpose, we have just created sample wrapper connection that just map all the required properties/methods to underneath SQL connection respective properties/methods:
Example: In XDConnection:
```
public override string Database
{
// _baseConnection is the object SQL Connection
get { return _baseConnection.Database; }
}
```
After all these:
1. I am not able to create the database using the customized connection class
2. I can create a database using in-built SQL Connection. But, I am still not able to use the customized connection class, it throws error while saving the data
I have posted complete sour code and exception [here](https://entityframework.codeplex.com/discussions/442427)
Please provide me the solution so that I can use EF in my project.
Regards,
Hitesh
Comments: Thanks RoMiller for your quick response. This also looks like a bug to me, it would be great if team can take it up along with other bugs...