Using the web.config connection string in a data access project

I am on .NET 4.6.1, Visual Studio 2015, the developer of SQL Server 2016. I want to specify my EF connection string in my main web.config project, but I can’t understand for the rest of my life as a link that in my project I have access to data. Every answer I can find says that it does the same thing as this one - that is, use this:

System.Configuration.ConfigurationManager
    .ConnectionStrings["connectionStringName"].ConnectionString;

However, when I use this, a valid connection string is not passed. Instead, it gets the default value; the data source is set to .\SQLEXPRESSwhen I do not have a line with the specified somewhere. The search for all files in the solution for SQLEXPRESSdoes not return results, so it is not encoded anywhere.

What is the correct way to pass my connection string from my main web.config (with the appropriate conversions for each assembly) to my data access project?

+4
source share
6 answers

app.config/web.config, , , . , - , , , web.config -. Windows wpf .., . app.config/web.config , , , .

, , MyDA.dll, app.config , MyDA.dll.config ( ), , MyDA.dll.config . , web.config app.config .exe.


Entity Framework DbContext . . web.config.

<connectionStrings>
    <add name="ConnectionName1" connectionString="Data Source=(local);Database=YourDB;User Id=YourUser;Password=yourPassword" providerName="System.Data.SqlClient" />
    <add name="ConnectionName2" connectionString="Data Source=(local);Database=YourDB2;User Id=YourUser2;Password=yourPassword2" providerName="System.Data.SqlClient" />
    <!-- more here -->
</connectionStrings>

Entity Framework DbContext, .

var myEntities = new MyEntities("ConnectionName1"); // MyEntities is some class that derives from DbContext

DbContext, , . , DbContext , . DbContext , , web.config app.config .

public class MyEntities : DbContext {
    public MyEntities() : base("ConnectionName1") {}
}

// calling code now never has to know about the connection
using(var myEntities = new MyEntities()) { /* do something here */}

, , System.Configuration ( , ) .

var connection = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionName1"];
var conString = connection.ConnectionString; // you can also get the provider
var myEntities = new MyEntities(conString);

, , , .

+4

. \SQLEXPRESS, -. SQLEXPRESS , .

. :

<clear />

.

. \SQLExpress 126 machine.config, IMHO , <clear />.

+1

Server Explorer → Data Connections ? , , ADO.Net Access. , .

0

/ , MSDN:

var connectionString = ConfigurationManager.ConnectionStrings["connectionStringName"].ConnectionString;

, :

  <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
        <startup> 
            <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
        </startup>
        <connectionStrings>
          <add name="connectionStringName" connectionString="Data Source=(LocalDB)\v11.0;Initial Catalog=DatabaseName;Integrated Security=True;Pooling=False" />
        </connectionStrings>
    </configuration>

:

System.Configuration

: https://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.connectionstrings(v=vs.110).aspx

0

, EF , , , , MyContext, app/web.config. EF ( ADO.Net), , DLL .config.

, -, .config , :

  • web.config - DEVELOPMENT.
  • web.staging.config - replace STAGING.
  • web.release.config - replace RELEASE.

, !

0
 <configuration>
    <connectionStrings>
      <add name="CONNECTIONSTRINGNAME" connectionString="server=.; Database=YOURDATABASENAME;Integrated Security=true;" providerName="System.Data.SqlClient"/>
    </connectionStrings>
 </configuration>

0

All Articles