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

  • Home
  • SEARCH
  • 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 3458452
In Process

The Archive Base Latest Questions

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

I have the same data model on a bunch of different servers. I want

  • 0

I have the same data model on a bunch of different servers. I want to dynamically create a connection string based on who my user is and what they are doing.

My users can have multiple databases on multiple servers. I need a clean way to build a connectoin string when I create my DomainService.

I see that the DomainService has an override (inherited from LinqToEntitiesDomainService) called CreateObjectContext() that would allow me to set any connection string I want, then return the new entity and life is good. The problem is, the CreateObjectContext() gets called after the constructor, so I can’t set a string via an invoke method. Also, I’ve tried to create a new parameterized constructor on the DomainService, but it never gets copied to the DomainContext on the client.

The CreateObjectContext() would work great if I was able to pull my connection string, but since I have to use data from the client to figure out which DB to connect, this obviously won’t work.

The more I think about it, the more I feel a custom constructor is exactly what I need – just can’t figure out how to get that done.

What am I missing?

  • 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-18T09:59:05+00:00Added an answer on May 18, 2026 at 9:59 am

    I found a solution. For those that are interested, here it is:

    This feels a bit like a hack, but it’s the only solution I could come up with. Thanks to Sally Xu over at forums.silverlight.net for the ideas.

    Since each of my users can have mulitiple databases on multiple servers, I needed to find a way to create the ConnectionString before the DomainService was used the first time. Each time the user selects a new project from the UI, I set a cookie like this:

    private void SetCookie(string cookieName, string cookieValue)
    {
      DateTime expireDate = DateTime.Now + TimeSpan.FromDays(1); 
      string newCookie = cookieName + "=" + cookieValue + ";expires=" + expireDate.ToString("R");
      HtmlPage.Document.SetProperty("cookie", newCookie);
    }
    

    The cookieName is SelectedProjectId and the cookieValue is the current selected project in my UI.

    Then I create a new DomainService as normal, but I override CreateObjectContext(). This method gets called the very first time you reference your DomainService object. My override looks like this:

    protected override ProjectEntities CreateObjectContext()
    {
      long projectId = -1;
      StringBuilder connection;
      if (System.Web.HttpContext.Current.Request.Cookies["SelectedProjectId"] != null)
      {
        projectId = Convert.ToInt64(System.Web.HttpContext.Current.Request.Cookies["SelectedProjectId"].Value);
      }
      else throw new Exception("Selected Project ID Exception");  // temporary
    
      // Verify this user has access to the DB just in case it's spoofed 
    
      // Lookup project ID in my database to get the database name and server name
    
      // Load template connection string found in web.config
      // Replace the template holders for SERVER_NAME and DATABASE_NAME with above lookup values
    
      return new ProjectEntities(MyDynamicConnectionString);      
    }
    

    Again, this is a bit hackish, but it was the only way I could find to dynamically create a connection string for my needs. I hope this helps someone else…

    • 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.