0

I've created a repository class and have the following code:

public class InventoryDA : Accident_Reporting_Entities
{

}

Web.config:

<add name="Accident_Reporting_Entities" connectionString="metadata=res://*/Models.IncidentModel.csdl|res://*/Models.IncidentModel.ssdl|res://*/Models.IncidentModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=server;initial catalog=Accident_Reporting;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />

<add name="Accident_Reporting_Entities2" connectionString="metadata=res://*/Models.IncidentModel.csdl|res://*/Models.IncidentModel.ssdl|res://*/Models.IncidentModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=server;initial catalog=Accident_Reporting2;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />

I'm wondering how I can have multiple connections and replace Accident_Reporting__Entities with another connection string. The connections are of different databases that have the same design. Is it possible to check for a session variable and change this connection according to the variable, and would this be the best way?

4
  • stackoverflow.com/help/mcve Commented Feb 18, 2019 at 15:02
  • could you explain what is wrong with the question? Commented Feb 18, 2019 at 15:04
  • You didn't explain anything about your base type Accident_Reporting_Entities, so answers you can get may vary a lot. Anyway you can implement two "different" DbContext types which you setup using two different connection strings. And then your logic will decide which context to use. Will it be http session, http request, user input or something else, it's of course up to you :) Commented Feb 18, 2019 at 15:08
  • This sounds like the answer I'm looking for, but I'm not the most experienced with MVC. Could you give a simple example so I can see the syntax of using two DbContext types? Commented Feb 18, 2019 at 15:12

1 Answer 1

2

You can use the same DbContext with multiple connection strings:

public partial class SchoolDBEntities : DbContext
{
    public SchoolDBEntities(string connectionString) : base(connectionString)
    {

    }
}

var db1 = new SchoolDBEntities("Server=myServerAddress;Database=myDataBase1;User Id=myUsername;Password=myPassword;");
var db2 = new SchoolDBEntities("Server=myServerAddress;Database=myDataBase2;User Id=myUsername;Password=myPassword;");

Or you can pass to the contructor the name of the key in the Web.config file:

<configuration>
    <connectionStrings>
        <add name="myDataBase1" connectionString="Server=myServerAddress;Database=myDataBase1;User Id=myUsername;Password=myPassword;" />
        <add name="myDataBase2" connectionString="Server=myServerAddress;Database=myDataBase2;User Id=myUsername;Password=myPassword;" />
    </connectionStrings>
</configuration>

var db1 = new SchoolDBEntities("myDataBase1");
var db2 = new SchoolDBEntities("myDataBase2");
Sign up to request clarification or add additional context in comments.

3 Comments

If I were to use a session variable to decide which connection string to use, would I need to do a check on the variable in every controller action?
Yes, or you can use a factory to keep the logic in one place.
Do you know of where I could see a good example of a factory?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.