I've deleted the connection string from my web.config and Entity Framework is still connecting to the database! Where is the connection string being set? This is an issue because I need to make the live version of my website point to the live database.

- 27,486
- 9
- 86
- 108

- 15,170
- 23
- 107
- 189
-
2create a connection string named exactly the same as the class name of your DbContext – Omu Apr 26 '11 at 18:09
-
I have. Why might this not work? – Ian Warburton Apr 27 '11 at 08:44
-
4Well I've found the problem. I was setting the name of the database in the context contructor and it appears that if one does that then that must also be the name of the connection string! Votes please... – Ian Warburton Apr 27 '11 at 09:05
-
@IanWarburton : So did you not write anything in web.config? [this](http://stackoverflow.com/questions/33993727/unable-to-access-remote-database-in-c-sharp) has the link to my problem. but i have tried your solution though not so clear.. – Vini Nov 30 '15 at 13:30
-
still no accepted answer?! I guess Omri's answer is correct. – Vahid Ghadiri Feb 26 '17 at 21:02
9 Answers
Here's a gotcha I found with the "convention over configuration" philosophy when it comes to trying to connect to existing databases (like you're doing).
If your DbContext class (e.g. Northwind) is in a namespace (e.g. MvcProject), for some reason EF won't match the name of the class with a connection string in web.config named "Northwind" (or "MvcProject.Northwind"), and then it will just create a connection string defaulting to the local SQLEXPRESS instance, with a database called "MvcProject.Northwind". This will be an empty database. And you'll break your head trying to figure out why you're getting no data back, until you realize that you're not connected to the right DB.
The way I got around this (not elegant but it's the quickest way I found to fix it): add a constructor to your DbContext class that calls the base with the name of the connection string in web.config - e.g.
namespace MvcProject
{
public class Northwind : DbContext
{
public Northwind() : base("Northwind") {}
}
}
Hope that helps someone out there ;-)

- 35,843
- 15
- 128
- 182

- 161
- 2
-
1The official documentation says you should use: base("name=Northwind") I found this to be the only way it works. If you test "Forthwind" you'll see it doesn't throw an exception, but "name=Forthwind" will. – Richard Aug 13 '14 at 12:50
-
I think this is my problem but still can you guide me with my question http://stackoverflow.com/questions/42475840/entityframework-keeps-referring-to-a-copy-of-my-actual-database – Samra Feb 27 '17 at 01:48
-
What if i want to use the connection string that's contained within a transform config file? When I run my update-database command it always uses the web.config file even if I've selected a custom configuration. – Ciaran Gallagher Jan 25 '18 at 10:05
You'll need something like this:
<configuration>
<connectionStrings>
<add name="MyContext"
connectionString="Data Source=.\SQLEXPRESS;Integrated Security=SSPI;Initial Catalog=YourDatabaseName"
providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
Or if your database resides is App_Data
folder:
<configuration>
<connectionStrings>
<add name="MyContext"
connectionString="Data Source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|YourDatabaseFilename.mdf;User Instance=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
Replace MyContext
with name of your class that extends DbContext
.

- 101
- 2
-
1I think I've done this. I've set the 'Server' value to an IP address and the name of the connection string to the name of the derived context class but it apppears to be just ignoring it! – Ian Warburton Apr 26 '11 at 15:42
The EF couldn´t find the connection for me but I used the connection string in the base():
namespace MvcProject
{
public class Northwind : DbContext
{
public Northwind() :
base("Data Source=servername;Initial Catalog=database;User ID=yourID;Password=yourPass;Trusted_Connection=False;") {}
}
}
Just to test the connection and it´s worked.

- 32,176
- 5
- 81
- 116

- 41
- 1
-
Yes it can work fine, but it is considered to be a bad practice to put the connectionstring inside the code, for exemple if you want to deploy your application in the production server, you need to change the connectionstring in your code, rebuild the solution and publish it again. – Mehdi Souregi Feb 02 '17 at 15:37
Convention > Configuration, right?
By default, EF Code fist will create a database in your local SQL express instance.

- 690
- 4
- 15
-
Yes I thought it might be something like that. So how do I get it to use the connection string in web.config because it appears to be ignoring it? – Ian Warburton Apr 26 '11 at 13:54
-
Is the connection string name in web.config the same as the name of the DbContext class? i.e. if you have `public class MyContext : DbContext` then your connection string needs to be named MyContext – Nathan Craddock Apr 30 '11 at 15:22
I faced the same problem and simply changed my connection string name as suggested in web.config file as the name of context db and all went well.

- 31
- 4
Right click on your Entity Framework mode (edmx file) > goto Properties. You'll see the connection string there.
If your Entity Model is in a separate project, then it must be in it's own settings file.

- 11,465
- 7
- 47
- 64
-
I haven't got an edmx file because I'm using Code First. I've done a search for 'connectionString' and it doesn't appear anywhere else in my solution! – Ian Warburton Apr 26 '11 at 12:47
If You are using codefirst aproach with DbContext you can place a connection string with name matching your context class name in your web.config and it will work just fine.

- 6,655
- 2
- 39
- 60
-
Doesn't seem to work for me. I added a connection string with some junk added to the 'Data Source' value and it kept on working... – Ian Warburton Apr 26 '11 at 14:52
-
it should be just a sql connection string and Name attribut should exactly match your context name – Alexander Taran Apr 26 '11 at 15:06
Connection strings with Database First Approach using EntityFrameWork 5.0 This is how it looks..
<add name="DbEntities" ConnectionString="metadata=res:///Models.DbEntities.csdl|res:/// Models.DbEntities.ssdl|res://*/Models.DbEntities.msl;provider=Oracle.ManagedDataAccess.Client;provider connection string="TNS_ADMIN =( Here we write the location where tns file is located) --example of tns file location is D:\app\client\oracle\product\12.2.0\client_2\network\admin\sample;
USER ID='';PASSWORD='';DATASOURCE='';PERSIST SECURITY INFO= True"" providerName="System.Data.EntityClient"/

- 29,388
- 11
- 94
- 103
-
As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 27 '22 at 15:35