简体   繁体   中英

Entity Framework Migration cant find connection string in App.config file

I have a second project in my solution called DataAccess (.NET Framework Class Library) for models. I set my main asp .net mvc project as start up and from package manager console I set DataAccess as default. I already have an existing database. So I installed Entity Framework in DataAccess layer using package-install command to get the latest. Then I added an Entity Data Model. But whenever I try to run enable-migrations from PM it shows that No connection string named 'MAHMHEntities' could be found in the application config file. Although there is a connectionString in App.config called MAHMHEntities . In my MAHMHEntities.cs class I have declared the connection string:

namespace DataAccess.Entities
{
  using System.Data.Entity;

  public partial class MAHMHEntities : DbContext
  {
    public MAHMHEntities()
        : base("name=MAHMHEntities")
    {
    }  
    //other dbset classes
  }  
}  

In DataAccess layer I have App.config file that has two connectionString, I added the DefaultConnection myself :

<connectionStrings>
  <add name="DefaultConnection" connectionString="Data Source=.\SQL2014;Initial Catalog=MAHMH;Integrated Security=SSPI" providerName="System.Data.SqlClient" />
  <add name="MAHMHEntities" connectionString="data source=.\SQL2014;initial catalog=MAHMH;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" />  
</connectionStrings>  

Whichever connection I use I always get the same error when I try to enable migration. Entity Framework version is 6.2.0.

You need to set Data access project as start up project. Or you can add your connection string to web application's(Which is currently start up project) web.config

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM