Autogenerated default connection string vs. manually added one

无人久伴 提交于 2019-12-01 17:46:23
Sergey Berezovskiy

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.

The configuration manager just grabs all of the connection strings defined in the app/web.config.

It can't make the generalized assumption that once you add a connection string you wouldn't want a default one around anymore. That second connection string might point to an entirely different database.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!