I am trying to publish my website on the intranet server. From websites and suggestion I understood that i had to change the connection string to point to the database in the intranet server. While trying to do that i understand that i do not have a connectionstring in my web.config. Or maybe I am not having something similar to what that has been mentioned in the websites. Should I have added the connection string explicitly before I started working on the project? But my project works fine in my local machine with the local database.
So is it possible that a project can work without a connection string when working with local database or am I missing something really important? Now when I need to publish the project i could add a connection string in the web.Config file? I have a connection string portion commented out in my web.release. Should i uncomment the connection string in web.release or i add a new connection string in web.config.
Web.Config
<configuration>
<configSections>
<section name="entityFramework" requirePermission="false" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</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.5" />
<httpRuntime targetFramework="4.5" />
<authentication mode="Windows" />
<authorization>
<deny users="?" />
</authorization>
</system.web>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<!-- So many dependent assembly -->
</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>
</configuration>
Web.Release.config
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<!--
<connectionStrings>
<add connectionString="Data Source=ReleaseSQLServer;Initial Catalog=MyReleaseDB;Integrated Security=True" name="MyDB" xdt:Locator="Match(name)" xdt:Transform="SetAttributes" />
</connectionStrings>
-->
</configuration>
I am using Visual Studio Express 2013 for web. Devloped application is a MVC5 web application. Database used Local DB. But i have copied the database into the intranet server using SSMS.
Update : added config details
Based on comments and answer i have added the following to my web.config
<entityFramework>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
<connectionStrings>
<add
name="TEDALS_Ver01.DAL.TedalsContext"
connectionString="Server=FE0VMC0643; Database=TeDaLSdev; "
providerName="System.Data.SqlClient" />
</connectionStrings>
And this is the connection string that i have in the properties of the database
Data Source=FE0VMC0643;Initial Catalog=TeDaLSdev;Integrated Security=True;Connect Timeout=15;Encrypt=False;TrustServerCertificate=False
So is it possible that a project can work without a connection string when working with local database or am I missing something really important?
If you're using Entity Framework (I don't know about other ORMs) and you don't specify a connection string it will internally create one pointing to LocalDB or SQLExpress by using the full name of your DbContext
.
It's explained in Entity Framework > Get Started > Connections and Models:
If you have not done any other configuration in your application, then calling the parameterless constructor on DbContext will cause DbContext to run in Code First mode with a database connection created by convention. […] uses the namespace qualified name of your derived context class […] as the database name and creates a connection string for this database using either SQL Express or LocalDb. If both are installed, SQL Express will be used.
If your database is not following the convention established by Entity Framework, then you either modify your database to have it following the convention or add a connection string for Entity Framework to use.
If your DbContext
is
namespace MySolution.MyApplication.Contexts
{
public class MyContext : DbContext
{
[…]
}
}
either you have a database named MySolution.MyApplication.Contexts.MyContext
in your (localdb)\MSSQLLocalDB
/ localhost\SQLExpress
instance or add the following connection string to your App.config
/ Web.config
:
<add
name="MySolution.MyApplication.Contexts.MyContext"
connectionString="Server=myServerAddress; Database=myDataBase; User Id=myUsername; Password=myPassword;"
providerName="System.Data.SqlClient" />