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

前端 未结 8 1009
故里飘歌
故里飘歌 2020-12-03 00:34

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 vi

相关标签:
8条回答
  • 2020-12-03 01:01
     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.

    0 讨论(0)
  • 2020-12-03 01:05

    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

    0 讨论(0)
  • 2020-12-03 01:10

    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!!

    0 讨论(0)
  • 2020-12-03 01:12

    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?

    0 讨论(0)
  • 2020-12-03 01:13

    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.

    0 讨论(0)
  • 2020-12-03 01:14

    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.

    0 讨论(0)
提交回复
热议问题