Manual default connection string with auto-generated by default

Let's say I have a simple WPF application that uses the Entity Framework Code First to create a database, connect to it, and display some data. From the very beginning I don’t want to worry about connection strings, so after adding a link to entityframework via Nuget, I get an automatically generated app.config as follows:

<?xml version="1.0" encoding="utf-8"?> <configuration> <configSections> <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --> <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> </configSections> <entityFramework> <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" /> <providers> <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /> </providers> </entityFramework> </configuration> 

I will run the test and watch the connection string:

 var strings = ConfigurationManager.ConnectionStrings; 

with the result:

 [0] = {data source=.\SQLEXPRESS;Integrated Security=SSPI;attachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true} 

As I like to define my own connection string, I will add this to my app.config:

 <connectionStrings> <add name="MyContext" connectionString="data ource=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\myDb.mdf;Integrated Security=True" providerName="System.Data.SqlClient" /> </connectionStrings> 

So, when I run the test again and watch the connection strings, I see that now there are two:

 [0] = {data source=.\SQLEXPRESS;Integrated Security=SSPI;attachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true} [1] = {data source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\myDb.mdf;Integrated Security=True} 

Why do I see two connection strings? If the first is the default, if it is not forgotten, as soon as I created it?

thanks

+6
source share
2 answers

The first connection string that you see comes from machine.cofig from your PC. It has the following section:

 <connectionStrings> <add name="LocalSqlServer" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient"/> </connectionStrings> 

Defines the default connection string for an ASP.NET database. If you really do not need this for your application, you can either edit the machine.config file (not recommended) or clear the connection lines before adding your own:

 <connectionStrings> <clear /> <add name="MyContext" connectionString="data ource=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\myDb.mdf;Integrated Security=True" providerName="System.Data.SqlClient" /> </connectionStrings> 

Also keep in mind - this connection string is not used by the Entity Framework. By default, it uses a SQLEXPRESS server and a database with a name equal to the full name of your DbContext class. You can check it by referring to context.Database.Connection.ConnectionString .

+7
source

The configuration manager simply captures all the connection strings defined in app / web.config.

It is not possible to make a generalized assumption that after adding a connection string you no longer want to use the default. This second connection string may point to a completely different database.

0
source

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


All Articles