EntityFramework - Where is the connection string?
Asked Answered
D

9

20

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.

Dingy answered 26/4, 2011 at 12:30 Comment(5)
create a connection string named exactly the same as the class name of your DbContextKeeler
I have. Why might this not work?Dingy
Well 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...Dingy
@IanWarburton : So did you not write anything in web.config? this has the link to my problem. but i have tried your solution though not so clear..Sorilda
still no accepted answer?! I guess Omri's answer is correct.Sinusitis
N
16

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

Nephograph answered 19/6, 2011 at 0:34 Comment(3)
The 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.Eau
I think this is my problem but still can you guide me with my question #42476340Larval
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.Hispidulous
O
10

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.

Ourselves answered 26/4, 2011 at 15:26 Comment(1)
I 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!Dingy
L
4

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.

Lafleur answered 4/8, 2011 at 15:7 Comment(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.Taxable
D
2

Convention > Configuration, right?

By default, EF Code fist will create a database in your local SQL express instance.

Dortch answered 26/4, 2011 at 13:14 Comment(2)
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?Dingy
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 MyContextDortch
P
1

Look in App.Config. It will store it there too.

Peggy answered 26/4, 2011 at 14:20 Comment(0)
S
1

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.

Sheryllshetland answered 14/7, 2015 at 18:34 Comment(0)
D
0

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.

Driving answered 26/4, 2011 at 12:33 Comment(1)
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!Dingy
U
0

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.

Urine answered 26/4, 2011 at 14:13 Comment(2)
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...Dingy
it should be just a sql connection string and Name attribut should exactly match your context nameUrine
T
0

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"/
Tapeworm answered 24/6, 2022 at 12:50 Comment(1)
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.Gorse

© 2022 - 2024 — McMap. All rights reserved.