Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EntityFramework - Where is the connection string?

Tags:

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.

like image 821
Ian Warburton Avatar asked Apr 26 '11 12:04

Ian Warburton


People also ask

Where is the connection string stored in Entity Framework?

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.

How do I get connection string?

Right-click on your connection and select "Properties". You will get the Properties window for your connection. Find the "Connection String" property and select the "connection string". So now your connection string is in your hands; you can use it anywhere you want.

Where does net core store connection string?

In ASP.NET Core the configuration system is very flexible, and the connection string could be stored in appsettings. json , an environment variable, the user secret store, or another configuration source.


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

like image 67
Omri Gazitt Avatar answered Sep 24 '22 17:09

Omri Gazitt


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.

like image 34
waz Avatar answered Sep 22 '22 17:09

waz