Changing connection string problem

2 posts, 0 answers
  1. Mark
    Mark avatar
    1 posts
    Member since:
    Sep 2010

    Posted 27 Feb 2014 Link to this post


    I have developed a multi-tenant application in Silverlight where multiple users use the same Silverlight application but all have separate databases.

    I have a Telerik Data Access domain service for each main entity (customers, orders etc) that handles all the CRUD operations from the Silverlight Client.

    Querying the data is no problem as I pass through an encrypted connection string token as one of the parameters to the method I want to query which works 100%. Where my problem is coming in is for Creating, updating and deleting records. The .tt service provides the following methods

    public void DeleteBookmark(Bookmark Bookmark)
                // This is a callback method. The actual Delete is performed internally.
            public void UpdateBookmark(Bookmark Bookmark)
                // This is a callback method. The actual Update is performed internally.
            public void InsertBookmark(Bookmark Bookmark)
                // This is a callback method. The actual Insert is performed internally.

    but the operation is happening much earlier.

    How do I change the connection string before the operation occurs?


  2. Kaloyan Nikolov
    Kaloyan Nikolov avatar
    118 posts

    Posted 03 Mar 2014 Link to this post

    Hello Mark,

    In order to use different connection strings for each database call you can override the CreateDataContext method. You should do that in a partial class of the target Domainservice in order to keep the customization in case you regenerate the service in feature. 

    protected override EntitiesModel1 CreateDataContext()
        var clientId = WebOperationContext.Current.IncomingRequest.Headers["ClientId"];
        var connStr = GetConnStrByClientId(clientId);
        return new EntitiesModel1(connStr);
        //return base.CreateDataContext();

    The above code shows how this could be done. You should create your data context manually and pass the connection string to its constructor. Now the question is how you know which connection string to be used for a given service request. Usually such problems are solved by passing additional information in the message header. The code sample above assumes that the Silverlight client application has added additional header to the message with name "ClientId". Here is a helpful article describing what should be done to implement this approach.

    You should know that once created the data context is used during the entire lifetime of the DomanService instance. So if you control somehow the lifetime of the service classes you would need to override the DataContext property and implement your own logic when to create a new data context.

    I would not suggest you to transport the connection string (even if it is encrypted) with each call. I would suggest you to implement token based authorization in your application. To do this when a given user is logged in the server should issue a token or the above sample this the clientId, then each call to the services should contain this token. The services should determine which connection string to use based on the passed token. This approach will work for the load operations and for the CUD operations.

    I hope this helps. Should you have any questions do not hesitate to get back to us. 

    Kaloyan Nikolov
    OpenAccess ORM is now Telerik Data Access. For more information on the new names, please, check out the Telerik Product Map.
  3. DevCraft banner
Back to Top