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 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:
The cookieName is
SelectedProjectIdand 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: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…