Silverlight Entity Framework Dynamic Connection String

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?


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

Need Your Help

Is there a substr method in jade template?

api node.js jade

Is there exist some string method in jade template?

Posting a form data using POST with PHP

php redirect curl

I have been looking into this for hours and still at a loss.