53

I just install EF 4.3 and trying to upgrade my project with migration. however I am getting issues with trying to execute add-migration initial to my project via Package Manager console.

It is throwing any exception now No connection string named 'MyApplicationEntities' could be found in the application config file.

Now my config has it all

<connectionStrings>
<add name="MyApplicationEntities" 
     connectionString="metadata=res://*/DataModel.csdl|res://*/DataModel.ssdl|res://*/DataModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=localhost;initial catalog=MyApplicationEntitiesDB;integrated security=True;multipleactiveresultsets=True;App=EntityFramework&quot;" 
     providerName="System.Data.EntityClient" />

I am not sure what is the issue is it a bug in EF 4.3 or there is something I am not doing right.

I thought this post has solved the issue but not quite.

Anyone got an answer.

Appreciate Sanj.

1

10 Answers 10

110

Ah, figured this out accidentally.

I had to remove

public MasterEntities()
    : base("name=MyApplicationEntities")
    //      ^^^^^
{
}

to

public MasterEntities()
    : base("MyApplicationEntities")
{
}

EF 4.3 does not like connection string being called name=xxxxx

12
  • 5
    do you have a connectionstring in your web.config or app.config with the corresponding Name? This was a common mistake I made initially since my Context Object was in a different DLL, the connection info is not automatically copied across to the main project.
    – Sanj
    Commented Oct 25, 2012 at 2:49
  • 8
    That won't work as you think so. If you remove the "Name=" part it will create a database using that name :)
    – Timotei
    Commented Nov 8, 2012 at 19:20
  • 9
    This is probably not going to be the solution anyone is looking for--using "name=" in this way is how you indicate that you're doing database-first, whereas removing it will signal code-first and has a good chance of throwing UnintentionalCodeFirstException.
    – bwerks
    Commented Apr 14, 2013 at 19:38
  • 8
    Helped also in EF 6.0 - didn't find anything better.
    – makciook
    Commented Dec 14, 2013 at 18:07
  • 4
    PLEASE DON'T APPLY this change. This is not a correct solution.I have got a "UnintentionalcodefirstException" as bwerks mentioned.
    – Dhanuka777
    Commented Nov 4, 2014 at 6:03
43

The solution as Sanj pointed out is that you need to copy the connection string from your database project's App.config to the web project's web.config. I'm not sure why the above answer is marked as correct. I'm adding this as an answer instead of a comment so future readers will spot this.

3
  • 1
    i know this works but i have a class library project and doesnt have a config file Commented Feb 12, 2014 at 16:49
  • 1
    My post applies to web apps, but perhaps this link will help you. youtube.com/watch?v=rYK0B1CQUN8 You may need to add the information inside the class, if you created a data class and chose to not store the configuration in a config file.
    – Bill
    Commented Feb 12, 2014 at 21:14
  • Thanks! saved me probably 2 years and 35 minutes stupid researches! <3
    – Maximosaic
    Commented May 12, 2014 at 21:37
31

I had the same error but I already had a web.config file with the correct connection string name and a DbContext declared correctly. However, I noticed when I ran add-migration with -Verbose it state the 'Startup Project' as a different project than the one containing my context. So I change the Startup Project, re-ran the add-migration and it all worked!!

5
  • While running on EF 6.0 this solution worked for me. Here is another page I found which provides some more context: entityframework.codeplex.com/workitem/974
    – J.Hogan
    Commented Mar 25, 2014 at 20:33
  • Thanks -- also, after setting it as the Startup Project, you may need to run Enable-Migrations with the -force parameter if you have gotten it into a partially configured state. Remember to backup any code in the Migrations folder first as it will get wiped. Learn the hard way and thank goodness for source control. Commented Jul 4, 2015 at 21:49
  • This is the one that worked for me. I can't believe I overlooked that Commented Dec 6, 2016 at 22:28
  • I had similar issues. After reading this, I realized I had selected a different project in the solution as the startup project as I was doing some test. Commented Jan 13, 2017 at 16:00
  • That's a very important answer, because it solves the non-obvious issue, when everything seems right. Also, important lesson to remember about the verbose mode :)
    – Bartosz
    Commented Dec 30, 2019 at 21:23
8

Make sure your statup project config file has the connection string.This link may help you.

3
  • Links tend to change or go missing, could you explain some of the contents or quote it here?
    – abarisone
    Commented Jun 16, 2015 at 6:34
  • Just check your Startup Project config file has the connection string or not.Possible scenario is when you put your edmx file in a class library project and it is referenced in a different project .So the project set as startup should have the connection string. Commented Jun 16, 2015 at 10:58
  • Argh! Thanks @SantoshPanigrahy. Another great Microsoft error Commented Sep 15, 2016 at 7:30
7

I also had this problem and solved it by

  1. Selecting the correct StartUp project.
  2. Rerunning the command on Package Manager Console.

Things worked out as expected.

3

I also encountered the similar exception. AppConfig is originally gets created in the project that we generate the entity model. But if you are executing the application using some other project (there are several Projects in my solution), the AppConfig needs to be included in the project which is being executed.

1
 1. ctor => Context

   public MasterEntities()
    : base("ConnectionStringName")
{
}

 2. config file

   <add name="ConnectionStringName"
      connectionString="Data Source=.;Initial Catalog=DatabaseName;User Id=sa; Password=YourPass;"
      providerName ="System.Data.SqlClient" />

 3. in Sulation Exporer right click the project and select 'Set as
    startup project'

 4. in PackageManagerConsole Change Default Project to Your Project of
    context class.

 5. then:

add-migration new

or added ConnectionString to config file of working Project.

1
  • Set as Startup Project helped me! Thank you! Commented May 26, 2021 at 9:49
0

In my case, i got two projects:

enter image description here

I just have to copy the connection string from DAL App.Config project to WPF App.Config project

0

If you get this error and you're working with Oracle DBs in .NET, check your existing <connectionStrings> tag in the startup project (web.config in web projects, app.config in console/Windows projects), and make sure Oracle didn't replace it with nonsense.

Installing the Oracle.ManagedDataAccess.* assemblies from NuGet does this obnoxious thing where it deletes whatever <connectionStrings> tag that already existed in destination project and replaces it with a new one at the bottom containing a boilerplate Oracle connection string.

Do a git diff or whatever you use to diff changes and focus on your *.config files. Restore old connection strings as needed, then rebuild and try again.

For all others, just remember that the <connectionStrings> tag is only read from the startup project's configuration (aka the entry assembly). If you have your DB stuff in another project that is referenced by your startup project, and you have an app.config in that DB project with a <connectionStrings> tag in it, that tag is only used for for scaffolding EF stuff. After that, the DB project reads the tag from the startup project's configuration instead of its own.

-1

For anyone arriving here because they are getting this error while working with WPF in Visual Studio, please take a look at this post: Does MVVM stop the ability for the Visual Studio Designer to show xaml?

Not the answer you're looking for? Browse other questions tagged or ask your own question.