Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8271725
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T06:55:13+00:00 2026-06-08T06:55:13+00:00

My ASP.Net MVC application has to connect to multiple databases at run time. I

  • 0

My ASP.Net MVC application has to connect to multiple databases at run time. I can overload my class to accept the connection string at run time as shown below

class MyClassDBContext:DbContext
{
  public MyClassDBContext(string str) : base(str)
  {
    this.Database.Connection.ConnectionString = str;
  }
}

Currently, I am retrieving this connection string from a database table. My workflow is as follows

  1. Website connects to default database using credentials stored in
    web.config
  2. Website queries default database to get connection strings for
    other databases.
  3. Websites connects to other databases by supplying the connection
    string at run time

The problem I facing right now is in keeping my code clean. Every time I need the connection string for database number 2, I have to look it up in the default database. Is there any cleaner way of doing this? I considered storing the connection string in the profile data but I am not sure if this is a good idea. Every user of my website will need to connect to at most 2-3 different databases depending on their credentials.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T06:55:15+00:00Added an answer on June 8, 2026 at 6:55 am

    I would personally put all connection strings in your App.Config file and use a simple IOC implementation.

    Actually the ninject package off Nuget might be perfect for your needs.

    Here’s what I mean though. Hopefully this makes your code clean. I used this exact same pattern for a previous project and it worked out well.

    You could take it a step further and make a Service Locator and register services in your global.asax. Let me know if that interests you. Also check out ninject.

    public interface IService() 
    { 
      string GetConnectionString(); 
      void DoStuff(); 
    }
    
    public class DBServiceOne : DbContext, IService
    {
      protected string GetConnectionString() 
      {
        return ConfigurationManager.AppSettings["DBServiceOneConnectionString"]
      }
    
      public DBServiceOne(string str) : base(str)
      {
         this.Database.Connection.ConnectionString = GetConnectionString()
      }
    
       public void DoStuff() { //logic goes here }
    }
    
    public class DBServiceTwo : DbContext, IService
    {
    
        public DBServiceTwo(string str) : base(str)
        {
          this.Database.Connection.ConnectionString = GetConnectionString();
        }
    
    
        protected string GetConnectionString() 
        {
          return ConfigurationManager.AppSettings["DBServiceTwoConnectionString"]
        }
    
        public void DoStuff() { //logic goes here }
    }
    
    public class DBServiceThree : DbContext, IService
    {
    
       public DBServiceThree(string str) : base(str)
       {
         this.Database.Connection.ConnectionString = GetConnectionString();
       }
    
       protected string GetConnectionString() 
       {
         return ConfigurationManager.AppSettings["DBServiceThreeConnectionString"]
       }
    
       public void DoStuff() { //logic goes here }
    }
    

    Now for the implementation — Use Constructor Injection on your controllers

    //This could be in your home controller
    
    public class HomeController : AsyncController
    {
        private IService DBOneService;
        private IService DBTwoService;
        private IService DBThreeService;
    
       public HomeController(IService one, IService two, IService three)
       {
          DBOneService= one;
          DBTwoService = two;
          DBThreeService = three;
       }
    
      public HomeController() : this(new DBServiceOne(), new DBServiceTwo(), new DBServiceThree()) {}
    
    public ActionResult Index() {
       DBOneService.DoStuff(); //here you'd want to return a list of data and serialize down with json or populate your razor template with it. Hope this helps!
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am developing an asp.net mvc application, which has these enity classes: public class
My ASP.NET MVC application has pages with attachments and these attachments can be of
i have asp.net mvc application where my model has a relation like Question can
I have an ASP.NET MVC application that has one part where I dont really
I am creating an ASP.NET MVC application that has postcode lookup functionality. I capture
I have an ASP.NET MVC 3 application which has a post action called Create
In my ASP.NET MVC application, I'm trying to figure out whether the user has
I have an Asp.net MVC application running on AppHarbor. It has the everyday email
We are writing an ASP.NET MVC application. By default, if the client browser has
I'm trying to create ASP.NET MVC Application with Entity Framework, which has One to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.