21

All my Razors views have this error:

The pre-application start initialization method Start on type WebMatrix.WebData.PreApplicationStartCode threw an exception with the following error message:

The entry 'DefaultConnection' has already been added.

Now, as you can see in my connection string configuration, I don't have the connection string name repeated:

<connectionStrings>
    <add name="DefaultConnection" connectionString="Data Source=.\foo;Initial Catalog=aspnet-foo-20130212133051;MultipleActiveResultSets=true;Integrated Security=SSPI" providerName="System.Data.SqlClient" />
    <add name="ServiDeskConnection" connectionString="Data Source=fooo;Initial Catalog=mdb;User Id=foo;Password=fooo;MultipleActiveResultSets=true" providerName="System.Data.SqlClient" />
    <add name="mdbEntities" connectionString="metadata=res://*/Models.ExternalData.ExternalContext.csdl|res://*/Models.ExternalData.ExternalContext.ssdl|res://*/Models.ExternalData.ExternalContext.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=foo;initial catalog=mdb;user id=foo;password=foo_prod01;multipleactiveresultsets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
    <add name="SisInfoInventoryEntities" connectionString="metadata=res://*/Models.ExternalData.Inventory.csdl|res://*/Models.ExternalData.Inventory.ssdl|res://*/Models.ExternalData.Inventory.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=.\foo;initial catalog=foo;integrated security=True;multipleactiveresultsets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
</connectionStrings>

I already tried to use <Clear/>, but it is worse, because it said that I don't have any connection.

I tried to unload the project of the same solution, but it stills did not work.

Note that when I build the project, it works well.

(This has been answered several times, but none of the solutions had worked for me.)

Community
  • 1
  • 1
Jorge
  • 17,896
  • 19
  • 80
  • 126

1 Answers1

55

After the <providers> and before the first <add....> node, add a <clear /> node. That will fix it!!

Or it can be in web.config that is parent to this one. Just add the below code before you add it again.

<remove name="DefaultConnection" />

Alternatively, clear all connection strings using

<clear />

I would suggest you rename DefaultConnection to something else like MyAppServices.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Neel
  • 11,625
  • 3
  • 43
  • 61
  • 1
    Sorryy but first I don't have declare any providers and already tried the clear but it doesn't work – Jorge Apr 01 '14 at 04:55
  • ok and Also worth checking that you do not have the Connection String added at a higher level, e.g. the MACHINE.CONFIG file. @Jorge – Neel Apr 01 '14 at 04:58
  • Checked too but no, it does not have a connection with the same name – Jorge Apr 01 '14 at 05:02
  • I would suggest you rename DefaultConnection to something else like MyAppServices @Jorge – Neel Apr 01 '14 at 05:03
  • `` dirty and naughty little fix that - especially if multiple developers work on the same file and end up breaking your environment every time they check in some bogus `web.config`. This fix did wonders for my local environment – Eon Jul 13 '15 at 12:47
  • Just changing the name to MyAppServices worked for me - but now I have a 404 error :-( – Tim Jul 12 '16 at 10:06