locked
Dynamic Connection String using Entity Framework RRS feed

  • Question

  • I have a Silverlight app that contains a tree control.  Each node on the control is a different project. Each project is stored on one of many database servers and could be named anything.  I need to find a way to dynamically create a connection string for my DomainService to use.

    My plan was to override CreateObjectContext() in the DomainService, but I am unable to pass in the data needed to create the connection string since the default connection string (from web.config) is used during construction.

    I need a way to be able to have my DomainService use a custom connection string with a few bits of data provided from the cleint.  I'm not concerned with security here since I am performing security checks before connecting to the database on the server.

    I'm stumped.  Any ideas?

    -Scott

    Saturday, December 4, 2010 3:21 PM

Answers

  • I'm pretty sure it works, because I used it in one case in my app.

    try this:

    Put this code in your Page's constructor:

    public partial class MainPage : UserControl
        {
            public MainPage()
            {
                InitializeComponent();

                DateTime expireDate = DateTime.Now + TimeSpan.FromDays(7); // set expiration date

                string newCookie = "MyCookie=1;expires=" + expireDate.ToString("R");
                HtmlPage.Document.SetProperty("cookie", newCookie);  // The code I posted earlier had a problem in this line. this should be the correct one

           }

    Put this code in your service:

    protected override TestEntities CreateObjectContext()
            {          

                if(  System.Web.HttpContext.Current.Request.Cookies["MyCookie"] != null)

                {
                     string cookie = System.Web.HttpContext.Current.Request.Cookies["MyCookie"].Value;

                }
                return base.CreateObjectContext();
            }

    It is like you set cookie in JavaScript. The cookie will be sent to server with the HttpRequest.


    Saturday, December 4, 2010 9:35 PM

All replies

  • My plan was to override CreateObjectContext() in the DomainService, but I am unable to pass in the data needed to create the connection string since the default connection string (from web.config) is used during construction.


    To override the default connection string in the web.config, you just need to provide your own connection string in the CreateObjecContext function:

    protected override YourEntityObjectContext CreateObjectContext()
            {                                
                return new YourEntityObjectContext(YourEFConnectionString);
            }



    Saturday, December 4, 2010 4:00 PM
  • Sally,

    Yes, I realize this, but how do I pass the info into the DomainService so I can construct the connection string within CreateObjectContext()?  Remember, I have to use some data from the client to know which database I need to connect to. 

    Here's an example, supose I have a treeView with 3 nodes.  They are Project1, Project2 and Project 3.  The user cliecked Project2 and now I need to query the database.  I know that Project2 is on Server2 and the database name is MyDatabase (I keep this in the object tied to the treeView).  I now need to instantiate the DomainContext using a connection string with the Server2 server and MyDatabase as the target database.  I have no way to get this information from within CreateObjectContext().  Instead, I need to find a way to pass this information into the DomainService so that it's available by the time CreateObjectContext() gets called.

     

    -Scott

    Saturday, December 4, 2010 4:23 PM
  • What I can think of is to use a Cookie to store this info. When user click a node, you can set this info into a cookie in Silverlight before you call your service load.

    DateTime expireDate = DateTime.Now + TimeSpan.FromDays(7); // set expiration date
    string newCookie = "YourCookieName=" + SomeValue + ";expires=" + expireDate.ToString("R");

    HtmlPage.Document.SetProperty("YourCookieName", newCookie);

    HtmlPage.Document.SetProperty("cookie", newCookie);

    Then you can access that cookie in your Service function:

    Cookie cookie = System.Web.HttpContext.Current.Request.Cookies["YourCookieName"];

    Bases on this you can build your connection string.


    Saturday, December 4, 2010 7:39 PM
  • I like this idea, but unless I am missing something, I don't think it will work.  During my testing the HttpCookieCollection that gets returned on the server is always null.  How would the server know anything about cookies in the client browser?  Is there a way to get the session info at the server for the current client?

    Saturday, December 4, 2010 9:09 PM
  • I'm pretty sure it works, because I used it in one case in my app.

    try this:

    Put this code in your Page's constructor:

    public partial class MainPage : UserControl
        {
            public MainPage()
            {
                InitializeComponent();

                DateTime expireDate = DateTime.Now + TimeSpan.FromDays(7); // set expiration date

                string newCookie = "MyCookie=1;expires=" + expireDate.ToString("R");
                HtmlPage.Document.SetProperty("cookie", newCookie);  // The code I posted earlier had a problem in this line. this should be the correct one

           }

    Put this code in your service:

    protected override TestEntities CreateObjectContext()
            {          

                if(  System.Web.HttpContext.Current.Request.Cookies["MyCookie"] != null)

                {
                     string cookie = System.Web.HttpContext.Current.Request.Cookies["MyCookie"].Value;

                }
                return base.CreateObjectContext();
            }

    It is like you set cookie in JavaScript. The cookie will be sent to server with the HttpRequest.


    Saturday, December 4, 2010 9:35 PM
  • Success!!

    The problem turned out to be the HtmlPage.Document.SetProperty("cookie", newCookie); line.  I thought the property name was what the cookie was called, not the type of property being set.  Since I did not have it set to "cookie", it was not adding it to the cookie collection.

    This should solve my problem.  It feels a little like a hack since I think this should be able to be done in the DomainService direclty, but you have ended 3 days of pain for me. 

    Thank you very much!

    -Scott

    Saturday, December 4, 2010 9:58 PM
  • You can use session as well. 

    Take a look at this thread http://forums.silverlight.net/forums/t/196501.aspx

    Sunday, December 5, 2010 3:43 AM