1

Are there any .netTiers ninjas out there that know how to change the database connection at run time?

I've found the following article on their documentation: http://www.nettiers.net/DataLayer.ashx

Towards the end, it has a code snippet demonstrating how to do this, but I've followed this snippet exactly, and it still doesn't seem to work.

here's my code (names have been changed to protect the innocent :-):

DataRepository.AddConnection("localDynamic", "Data Source=myserver;Initial Catalog=myDB;Integrated Security=True");

TList<Patient> patients = DataRepository.Connections["localDynamic"].Provider.PatientProvider.GetAll();

and here is the web.config - the connection string in the web config is commented out so that I can test changing the connection string dynamically:

<configuration>
  <configSections>
    <section name="dcPlatinum.Data"
    type="dcPlatinum.Data.Bases.NetTiersServiceSection, dcPlatinum.Data"
    allowDefinition="MachineToApplication"
    restartOnExternalChanges="true" />
  </configSections>
  <!--<connectionStrings>
    <add name="netTiersConnectionString" connectionString="Data Source=myServer;Initial Catalog=myDB;Integrated Security=True" />
  </connectionStrings>-->

  <dcPlatinum.Data defaultProvider="SqlNetTiersProvider">
    <providers>
      <add
        name="localDynamic"
        type="dcPlatinum.Data.SqlClient.SqlNetTiersProvider, dcPlatinum.Data.SqlClient"
        connectionStringName="localDynamic"
        providerInvariantName="System.Data.SqlClient"
        entityFactoryType="dcPlatinum.Entities.EntityFactory"
        useEntityFactory="true"
        enableEntityTracking="false"
        enableMethodAuthorization="false"
        useStoredProcedure="false"

        />
    </providers>
  </dcPlatinum.Data>
  <system.web>
      <compilation debug="true" targetFramework="4.0" />
  </system.web>

</configuration>

The problem that is happening is that it is not finding a connection string with the "localDyanmic" key...

thoughts?

jac
  • 9,666
  • 2
  • 34
  • 63
Scot
  • 572
  • 1
  • 7
  • 27
  • can you please post the correct answer here.. i am looking for same thing – prasy Jan 30 '15 at 20:35
  • unfortunately, I don't have a correct answer. We ended up abandoning .net tiers for a home grown solution. If you find that the one answer below works for you, let me know and I'll mark it as the accepted answer. – Scot Feb 02 '15 at 16:04
  • To Someone who is looking for Answer to this question see it [here](http://nettiers.net/%28S%28shoyyl55dtdwuwiuu0kdz255%29%29/DataLayer.ashx). Go to Advanced Topics -->Dynamic Connection String. – prasy Feb 02 '15 at 21:11

1 Answers1

2

You've probably figured this out by now, but just in case you're still trying....

We have dynamic connections working in our current project and the code you posted looks fine to me.

But I think you still need to reference a connection string that is registered in the web.config from the provider config section. So uncomment the connection string and then change the provider section to reference 'netTiersConnectionString' instead of 'localDynamic'.

netTiers should should ignore this and use localDynamic at runtime.

Also, be sure to run the DataRepository.AddConnection method once per connection string when the application starts. We run ours in the Application_Start method of Global.asax

Rob Bird
  • 3,764
  • 2
  • 28
  • 38