Reputation: 1772
Hi I have a few projects in Solution. In project A, I have app.config with connection string called Ver3ConnectionString. But when I do debug, the connection string is not found in code:). While debugging connection strings contains one which is not defined in this app.config, nether in my app :). Im using VS2013 Express :).
Moreover, applicationSettings are ones which are defined in main project:), but not one which is being dubugged.
Magic:).
ADDED:
THe solution was created by my coworker from existing project, so Im looking for error in solution/project files.
MobileWalletContext class is connected with EntityFramework 6.X. Maybe this is the problem?
Added 2: This unknown connection string has name ="LocalSqlServer". Maybe this will be helpful?
App.config:
<?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>
<connectionStrings>
<add name="Ver3ConnectionString"
connectionString="Data Source=PAZI-PRO2\SQLEXPRESS;Initial Catalog=MobileWalletVer3;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="mssqllocaldb" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
</configuration>
C# code where connsterionString is called:
public MobileWalletContext() : base(string.Format("name={0}",ConfigurationManager.ConnectionStrings["Ver3ConnectionString"].ConnectionString))
{
Database.SetInitializer(new DropCreateDatabaseIfModelChanges<MobileWalletContext>());
}
Upvotes: 0
Views: 1035
Reputation: 7017
Name of the connection string is not likely to change. Therefore I would simplify context constructor to:
public MobileWalletContext() : base("Ver3ConnectionString")
{
Database.SetInitializer(new DropCreateDatabaseIfModelChanges<MobileWalletContext>());
}
That way I could define different connection string in each project using Ver3ConnectionString
name.
EDIT
DbContext
base constructor accepts both connection string name or full connection string.
In your case instead of referencing existing connection string you are just creating a new connection string with provided name and default settings. Thats why you are seeing LocalSqlServer.
If you pass name of existing connection string in config, it will be picked up and used.
EDIT
Make sure your connection strings are in app.config
or web.config
file in your main project directory.
Upvotes: 3
Reputation: 1734
I'd like to add this to what @Kaspars-Ozols wrote:
LocalSqlServer is your SQL Express installation and its configuration usually resides in a machine.config file like C:\Windows\Microsoft.NET\Framework\v4.0.30319\Config\machine.config
. You may have more than 1 machine.config [2 runtimes (2.0 and 4.0)]
x [2 Bitness (x86 and x64)]
makes 4 machine.config files!
<connectionStrings>
<add name="LocalSqlServer" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient"/>
</connectionStrings>
It's a good habit to use <clear />
at the very beginning of <appSettings>
or <connectionStrings>
sections.
<connectionStrings>
<clear/>
<add name="MembershipConnection" connectionString="Server=.;UId=some_user;PWd=P@$$w0rd;Database=DBName" providerName="System.Data.SqlClient" />
<add name="ElmahConnection" connectionString="Server=.;UId=some_elmah_user;PWd=P@$$w0rd;Database=DBName" providerName="System.Data.SqlClient" />
</connectionStrings>
Upvotes: 0