Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework Can't Find Connection String in Web.config

Entity Framework doesn't seem to actually be reading connection strings from the Web.config.

I started a new project and created a context:

public class FooContext : DbContext
{
    public FooContext() :
        base("Foo")
    {
    }

    // DbSets here
}

Then, I added a connection string to the projects Web.config:

<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="Foo" providerName="System.Data.SqlClient" connectionString="Data Source=Foo;Initial Catalog=Foo;Integrated Security=False;User Id=foo;Password=foo;MultipleActiveResultSets=True" />
  </connectionStrings>
  <appSettings>
      ...

I enabled migrations, generated an initial migration, and then attempted to update the database. After a while, the update fails saying that it couldn't connect to the database. So I pulled my project DLL into LINQPad and ran the following:

var context = new FooContext();
context.Database.Connection.ConnectionString.Dump();

And I get the following output:

Data Source=.\SQLEXPRESS;Initial Catalog=Foo;Integrated Security=True;MultipleActiveResultSets=True

It's trying to connect to LocalDB, completely ignoring my connection string. So I tried being more explicit in the context constructor, by using "name=Foo" instead of just "Foo".

public FooContext() :
    base("name=Foo")
{
}

For what it's worth, I've never had to do this before. I've even got other projects in the same solution where the I've simply passed the connection string name and they've been working fine.

I jump back over to LINQPad and run the code again, and now I get an exception:

No connection string named 'Foo' could be found in the application config file.

I'm at a complete loss. I've set up projects like this 100s of times and never had any issues. Since it may be important, I'm running the latest Entity Framework, 6.1.3. Any ideas what could possibly be going on here?

like image 859
Chris Pratt Avatar asked Apr 21 '15 15:04

Chris Pratt


2 Answers

I am assuming you are running this in Visual studio, make sure you are running your web project as a startup project to use web.config. If you are running another console or .tests project as startup, it will pick up their app.config files as config file.

like image 181
pjobs Avatar answered Sep 25 '22 21:09

pjobs


Please remember that EntityFramework searches connection strings in the config of the assembly which runs the code. If you for instance execute your EF methods using test methods by unit test runner then EF will search it in your tests assembly, not in the place where EF methods are stored. I believe it may be your case.

By the way - in EntityFramework 5, when Edmx is created it automatically generates DbContext it uses name=ConnectionString, not only ConnectionString, so it's I think okay.

like image 29
Arkadiusz Kałkus Avatar answered Sep 24 '22 21:09

Arkadiusz Kałkus