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 3393604
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T04:04:13+00:00 2026-05-18T04:04:13+00:00

We are currently developing an application based on NHibernate and ASP.NET MVC and a

  • 0

We are currently developing an application based on NHibernate and ASP.NET MVC and a SQL Server backend. Since I’m fairly new to NHibernate, I’m tryig to understand best practices.

Our application requires every user to have it’s own SQL Server database. These databases all have an identical structure.

Our customers are identified with a customercode, e.g. 1500.

We’ve come up with a custom connection provider for nHibernate, which we already use in our nServiceBus backend services:

public class DynamicConnectionProvider : DriverConnectionProvider
{
    public override IDbConnection GetConnection()
    {
        IDbConnection conn = Driver.CreateConnection();

        try
        {
            var messageExecutionContext = ServiceLocator.Current.GetInstance<ITTTContextProvider>().CurrentContext;
            if (messageExecutionContext.CustomerId == 0)
            {
                conn.ConnectionString = ConfigurationManager.ConnectionStrings["dev"]
                    .ConnectionString;
            }
            else
            {
                conn.ConnectionString = ConfigurationManager.ConnectionStrings["default"]
                    .ConnectionString
                    .FormatWith(messageExecutionContext.CustomerId);
            }

            conn.Open();
        }
        catch (Exception)
        {
            conn.Dispose();
            throw;
        }
        return conn;
    }
}

This connection provider checks the customer code in a context object and sets the connectionstring accordingly.

We are planning to provide a HttpContext aware ITTTContextProvider. For this I have two questions:

  1. How can we retrieve the customer code from the url and put it into our context object for every request? when we use the following route?

    <main-site-url>/{customercode}/{controller}/{action}/{id}

  2. Is this method of connecting to several identical databases valid or is it better practice to construct a sessionfactory foreach customer database?

  • 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-05-18T04:04:14+00:00Added an answer on May 18, 2026 at 4:04 am

    In order to get the customercode you need to access the route data, something along the lines of

    HttpContextBase currentContext = new HttpContextWrapper(HttpContext.Current); //ServiceLocator.Current.GetInstance<ITTTContextProvider>().CurrentContext; 
    RouteData routeData = RouteTable.Routes.GetRouteData(currentContext);  
    var cusomterCode = routeData.Values["customercode"]
    

    My second suggestion would be not to put this code in the above snippet provided. Abstract it away. See Joshua’s answer which highlights the approach I am thinking of.

    Can’t really help on the second question, actually not familiar with both frameworks mentioned.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.