0

I have a code first web.api project that was working fine with the automatically created .mdf in the App_Data folder. I decided to move my app to IIS and modify the app pool config to load the user profile, again, no problem. Then I loaded the .mdf in visual studios sql explorer and at that point, the app pool login from the web app started failing. No clue why and couldn't fix it, so I decided to use sql express instead of waste more time on it. So, I installed SQL Express, killed all my migrations, and modified the web.config connection string to:

<connectionStrings>
    <add name="DefaultConnection" providerName="System.Data.SqlClient" connectionString="Data Source=.;Initial Catalog=MyApp;Integrated Security=SSPI"/>
</connectionStrings>

When I try to use migrations to update-database or run the api app, I get an exception saying

Cannot attach the file 'C:\Users\Source\MyApp\Main\Solution\MyApp.API\App_Data\MyApp.DataService.DataModel.AppEntities.mdf' as database 'MyApp.DataService.DataModel.AppEntities'.

Well, that makes sense since I deleted it, but why is my app still trying to connect to the .mdf file? It's gone and I've changed the default connection. I've searched the solution for anything referencing the .mdf file, but nothing shows up. What am I missing?

sonicblis
  • 2,926
  • 7
  • 30
  • 48

1 Answers1

0

I finally figured out that I had to name my connection string the same as my DbContext class, so in my case:

<connectionStrings>
    <add name="AppEntities" providerName="System.Data.SqlClient" connectionString="Data Source=.;Initial Catalog=MyApp;Integrated Security=SSPI"/>
</connectionStrings>

to match my DbContext derived class

namespace MyApp.DataService.DataModel
{
    internal class AppEntities : DbContext
sonicblis
  • 2,926
  • 7
  • 30
  • 48