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> <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