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.

Community
  • 1
  • 1
Sanj
  • 3,770
  • 6
  • 26
  • 31
  • Possible duplicate of [No connection string named 'MyEntities' could be found in the application config file](http://stackoverflow.com/questions/12622408/no-connection-string-named-myentities-could-be-found-in-the-application-config) – jbtule May 17 '17 at 17:04

10 Answers10

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

shA.t
  • 16,580
  • 5
  • 54
  • 111
Sanj
  • 3,770
  • 6
  • 26
  • 31
  • I tried this to my Model.COntext.cs file, but it threw an exception. – micahhoover Oct 23 '12 at 16:13
  • 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 Oct 25 '12 at 02: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 Nov 08 '12 at 19:20
  • All it does is create a database of the same name, and in my case put it on a local installation of Sql Server Express! I would love to know the answer to this. – arame3333 Nov 30 '12 at 15:29
  • 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 Apr 14 '13 at 19:38
  • 8
    Helped also in EF 6.0 - didn't find anything better. – makciook Dec 14 '13 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 Nov 04 '14 at 06:03
  • 1
    @Dhanuka777 wow that is bit strong. Did you have a look at your DbContext implementation's OnModelCreating method.. your UnintentionalCodeFirstException is there clearly, you are not using code-first. – Sanj Nov 05 '14 at 00:28
  • In addition guys, simply work out what is the order of execution and look at the EF code itself, EF looks for fully decorated DBContext-Name with Context info, than falls back on ConnectionString names which is what I had specified above, followed by actual connection string being passed in. – Sanj Nov 05 '14 at 00:31
  • 1
    Hi Sanj, no offence mate, I have got lost on my way by doing this fix you suggested, removing "name". I am using "DB first". Once I did the change "Entities not found issue" dissapeard, butnow EF thinks it's codefirst and throwing various exceptions, like bwerks mentioned. I sorted out the root issue. Issue was that "Entities" connectionstring was not included in my webconfig, in the WCF project. I was new to WCF and tried to put the configuration in an app config. Rule of thumb is to check whether the appconfig/webconfig of the executing project has the "Entities" connection string. – Dhanuka777 Nov 09 '14 at 06:30
  • Thanks Sanj. Removing "Name=" helped me. – Ravi Kanasagra Jun 12 '15 at 10:49
  • #Solid! Thanks, this did it. EF6 – dunwan Jun 08 '16 at 08:33
  • I solved this problem differently. The code first migration looks for config files. The config file of startup project is given more value against your current project/dll with migrations. removing Name = empty will create a new database – Rishabh Jain Dec 14 '16 at 14:44
  • I guess the "Code First from database" template has a bug then, because by default it generates `base("name=...")`. – user247702 Jan 03 '18 at 16:37
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.

Bill
  • 2,382
  • 2
  • 24
  • 27
  • 1
    i know this works but i have a class library project and doesnt have a config file – Gautam Beri Feb 12 '14 at 16:49
  • 1
    My post applies to web apps, but perhaps this link will help you. http://www.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 Feb 12 '14 at 21:14
  • Thanks! saved me probably 2 years and 35 minutes stupid researches! <3 – Maximosaic May 12 '14 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!!

Phil
  • 2,232
  • 1
  • 26
  • 35
  • While running on EF 6.0 this solution worked for me. Here is another page I found which provides some more context: http://entityframework.codeplex.com/workitem/974 – J.Hogan Mar 25 '14 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. – Timothy Lee Russell Jul 04 '15 at 21:49
  • This is the one that worked for me. I can't believe I overlooked that – xerotolerant Dec 06 '16 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. – jimfromthegym - Jim Mackin Jan 13 '17 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 Dec 30 '19 at 21:23
8

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

  • Links tend to change or go missing, could you explain some of the contents or quote it here? – abarisone Jun 16 '15 at 06: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. – Santosh Panigrahy Jun 16 '15 at 10:58
  • Argh! Thanks @SantoshPanigrahy. Another great Microsoft error – Nick Devereaux Sep 15 '16 at 07: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.

Ram
  • 3,092
  • 10
  • 40
  • 56
xibabababa
  • 71
  • 1
  • 1
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.

Dhanuka777
  • 8,331
  • 7
  • 70
  • 126
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.

MohammadSoori
  • 2,120
  • 1
  • 15
  • 17
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

Renan Duarte
  • 138
  • 1
  • 10
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.

J.D. Mallen
  • 4,339
  • 3
  • 22
  • 33
-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?

Telos
  • 1,039
  • 1
  • 12
  • 20