Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Autogenerated default connection string vs. manually added one

Let's say I have got simple WPF application using Entity Framework Code First to create database, connect to it and display some data. From start I do not want to worry about connection strings so after adding entityframework reference via Nuget I'll get auto generated app.config looking like this:

<?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'll run test and observe connection string:

var strings = ConfigurationManager.ConnectionStrings;

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

And so when I run the test again and observe the connection srings I can see that there are two now:

[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 is it that I can see two connection string? If the first one is default, should it not be forgoten once I've created one?

Thanks

like image 537
vidriduch Avatar asked Jun 20 '14 15:06

vidriduch


People also ask

How to protect connection string?

The best way to secure the database connection string is to encrypt the value within the configuration file. The application would then load the encrypted value from the config file, decrypt the value, and then use the decrypted value as the connection string to connect to the database.

How do I change the EDMX connection string?

config and comment on the connection string (save file) Open the edmx (go to properties, the connection string should be blank), close the edmx file again. Open the app. config and uncomment the connection string (save file)

How do I change my connection string?

Select the TableAdapter or query that has the connection you want to edit. In the Properties window, expand the Connection node. To quickly modify the connection string, edit the ConnectionString property, or click the down arrow on the Connection property and choose New Connection.


1 Answers

First connection string which you see comes from machine.config from your PC. It has 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>

Which defines default connection string for ASP.NET database. If you really don't need it for your application, you can either edit machine.config file (not recommended) or clear connection strings before adding yours:

<connectionStrings>
    <clear />
    <add name="MyContext" 
         connectionString="data source=(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 Entity Framework. By default it uses SQLEXPRESS server and database with name equal to full name of your DbContext class. You can check it by accessing context.Database.Connection.ConnectionString.

like image 56
Sergey Berezovskiy Avatar answered Sep 23 '22 09:09

Sergey Berezovskiy