locked
Dynamic set connection string by client application RRS feed

  • Question

  • I have 5 sql server had sam db schema.
    I want to use ado.net data service with entity framework to access the dabase.
    but i want set sql connection string by client application.

    how can i do?

    [System.ServiceModel.ServiceBehavior(IncludeExceptionDetailInFaults = true)]
    public class TmicWebDataService : DataService<MyEFContext>
        {     
            public static void InitializeService(IDataServiceConfiguration config)
            {
                config.SetEntitySetAccessRule("*", EntitySetRights.AllRead);
                config.SetServiceOperationAccessRule("*", ServiceOperationRights.All);; 
            }   
    
           protected override MyEFContext CreateDataSource()
           {
               MyEFContext  context = new  MyEFContext(//set this conntection string by client application) 
               return context;
            }
    }

     

    Wednesday, July 15, 2009 9:01 AM

Answers

  • Ok just figured it out, you can add a custom header to your dataservicecontext:

              
                MyEntity svc = new MyEntity Uri("http://localhost:3356/Test.svc/"));

                svc.SendingRequest += delegate(object sender, SendingRequestEventArgs args)
                {

                    ((HttpWebRequest)args.Request).Headers.Add("HEADERNAME", "HEADERVALUE");

                };

    Thursday, July 23, 2009 2:47 PM

All replies

  • To pass the database name to the data service , I would pass it in a header so that it’s not visible in the URI .

     

    To switch the database at runtime , you can override the CreateDataSource method and switch the database that the provider connects to ,

    Below is an example of doing this with the AdventureWorks database with Entity Framework as the provider :

     

      protected override AdventureWorksEntities CreateDataSource()

            {

                string databaseName = WebOperationContext.Current.IncomingRequest.Headers["ThisHeaderContainsTheDatabaseName"].ToString();

                EntityConnection entityConnection = new EntityConnection();

                entityConnection.ConnectionString = "ConnectionStringConnecting to the  databaseName";

                //set other proeprties on the entityConnection

                AdventureWorksEntities dataContext = new AdventureWorksEntities(entityConnection);

                return dataContext;

             }


    Phani Raj Astoria http://blogs.msdn.com/PhaniRaj
    Wednesday, July 15, 2009 5:06 PM
    Moderator
  • thank your reply.

    but i don't know. how to do assign "ThisHeaderContainsTheDatabaseName" var by client application?
    have any sample code?

    thank your help.

    Wednesday, July 15, 2009 11:19 PM
  • I would like to hear about this as well. I'm trying to accomplish something similar, specifiy database in the http request. My application spans multiple databases (dynamically created by the user/application), all databases are the same and can use the same entity model but connection strings are different. I've figured out how to dynamically set the connection string, but how do I convey this information in the URI (or http header), especially if writing LINQ to SQL queries vs the service reference where it constructs the URI automatically?
    Thursday, July 23, 2009 1:41 PM
  • Ok just figured it out, you can add a custom header to your dataservicecontext:

              
                MyEntity svc = new MyEntity Uri("http://localhost:3356/Test.svc/"));

                svc.SendingRequest += delegate(object sender, SendingRequestEventArgs args)
                {

                    ((HttpWebRequest)args.Request).Headers.Add("HEADERNAME", "HEADERVALUE");

                };

    Thursday, July 23, 2009 2:47 PM