user8264268
user8264268

Reputation:

Missing Connection String for publishing

I learning to make an MVC project and now I am trying learn how to publish it I have encountered this problem where I have to configure my Connection string and I dont have any( I think) or just simply missing in my web.config.

Do I have to add it or it can be found somewhere else?(Was trying to add one but I thought to ask first or do research to avoid more problem since im still not that good)

Web.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework"
  type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
  requirePermission="false"/>
</configSections>
<appSettings>
<add key="webpages:Version" value="3.0.0.0"/>
<add key="webpages:Enabled" value="false"/>
<add key="ClientValidationEnabled" value="true"/>
<add key="UnobtrusiveJavaScriptEnabled" value="true"/>
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.6.1"/>
<httpRuntime targetFramework="4.6.1"/>
</system.web>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
  <dependentAssembly>
    <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35"/>
    <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0"/>
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35"/>
    <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0"/>
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35"/>
    <bindingRedirect oldVersion="1.0.0.0-5.2.3.0" newVersion="5.2.3.0"/>
  </dependentAssembly>
</assemblyBinding>
</runtime>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
  <parameters>
      <parameter value="mssqllocaldb"/>
  </parameters>
</defaultConnectionFactory>
<providers>
  <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer"/>
</providers>
</entityFramework>
<system.web>
<customErrors mode="Off"/>
</system.web>
</configuration>

Upvotes: 0

Views: 91

Answers (1)

oopsdazie
oopsdazie

Reputation: 761

Add a section within your <configuration> element, parallel to your <configSections> tag, for example:

<configuration>
    <connectionStrings>
      <add name="DbConnection" connectionString="Data Source=(localdb)\v11.0;Initial Catalog=randomDatabaseName;Integrated Security=True" providerName="System.Data.SqlClient" />
    </connectionStrings>
</configuration>

In your app, you can call the database by using the declared name: DbConnection. And you can add as many connection strings to this section as you want.

To answer your question, you can actually construct the string yourself in the app whenever you need it or write it here in the config file. If you are using entityframework I believe it is eaiser to put the connection string in web.config.

Upvotes: 1

Related Questions