0

I have Persistence class library, which contains my DbContext class. It also contains app.config file, where I have predefined connection string like this:

  <connectionStrings>
    <add name="<Namespace1>.Persistence.AssessmentContext" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\AssessmentContext.mdf;Integrated Security=True"
   providerName="System.Data.SqlClient" />
  </connectionStrings>

Assuming that AssesmentContext has Namespace1.Persistence namespace.

When I try to enable-migrations using PM console, it gives me such an error:

An error occurred accessing the database. This usually means that the connection to the database failed. Check that the connection string is correct and that the appropriate DbContext constructor is being used to specify it or find it in the application's config file.

When I tried to debug it and put debug output into AssessmentContext ctor I found that the connection string is still using .\SQLEXPRESS data source.

Data Source=.\SQLEXPRESS;Initial Catalog=<Namespace1>.Persistence.AssessmentContext;Integrated Security=True;MultipleActiveResultSets=True

So what I am doing wrong? And why EF doesn't take my connection string from app.config?

dda
  • 6,030
  • 2
  • 25
  • 34
Jevgenij Nekrasov
  • 2,690
  • 3
  • 30
  • 51

1 Answers1

0

The above connection string entry must be copied to the app.config of the executable program if you are using desktop app or copied to web.config if you are using web app.

Hesham
  • 361
  • 2
  • 11