Finding a flexible way to specify the connection string used by ASP.NET membership

I am developing an asp.net web application and I am using ASP.NET Membership Provider. The application uses the membership scheme and all the necessary objects in the main database of the application. However, during development I have to switch to different databases for different development and testing scenarios. To do this, I have a file for the external connection strings section, as well as an external appsettings section, which allows me not to change the main web.config, but it’s easy to switch db by changing the settings only in the appsettings section.

My files:

<connectionStrings configSource="connections.config">
</connectionStrings>

<appSettings file="local.config">
    ....

ConnectionStrings looks as usual:

<connectionStrings>
  <add name="MyDB1" connectionString="..." ... />
  <add name="MyDB2" connectionString="..." ... />
  ....
</connectionStrings>

And local.config as below

<appSettings>
    <add key="ConnectionString" value="MyDB2" />

My code takes this connection string into account

web.config ,

<add name="MembershipProvider" connectionStringName="MyDB2" ...>
....
<add name="RoleProvider" connectionStringName="MyDB2" ...>

- , , db.

- appsetting db- db? "" ? , , - (, local.config)

, asp.net intio , , , , asp.net .

+3
3
+1

. ( ) Visual Studios .

+1

, .

, local.config, . ,

, ( botrh my DAL ), :

  • web.config , DAL, :

    < add key = "ConnectionString" value = "ManagDB" /" >

  • , ,

    < add name= "MembershipProvider" connectionStringName = "ManagDB" ... >
    < name= "RoleProvider" connectionStringName = "ManagDB" ... >

Web.config , .

  1. , , web.config

    < connectionStrings configSource = "connections.config" > </ConnectionStrings>

  2. connections.config

    <ConnectionStrings>
    <add name = "MyDB" connectionString = "Data source = ..." providerName = "System.Data.SqlClient" / "> </ConnectionStrings>

Using this approach, I was able to have a single, immutable, application-specific web.config common to all instances, and then a specific connection string for a specific instance that allows DAL and membership to use the same database

0
source

Source: https://habr.com/ru/post/1737296/


All Articles