Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the purpose of ApplicationServices connection string in Entity Framework Code First scenario?

Whenever you create an application using EF code first you can see follwing web.config key added:

<add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient" />

You can easily see that it contains no application specific information. Changing those values do not affect how application runs, so it makes me think it must be some kind of unnecessary EF artifact, possibly required by the designer. However if you remove this key completely application will throw ConfigurationError on start. What is the real purpose of this key?

like image 601
Sebastian K Avatar asked Dec 30 '12 03:12

Sebastian K


People also ask

What is connection string in Entity Framework?

A connection string contains initialization information that is passed as a parameter from a data provider to a data source. The syntax depends on the data provider, and the connection string is parsed during the attempt to open a connection.

Where does Entity Framework store connection string?

Right click on your Entity Framework mode (edmx file) > goto Properties. You'll see the connection string there. If your Entity Model is in a separate project, then it must be in it's own settings file.

What is connection string in application?

Applications use connection strings to identify the server instance and database to connect to and to determine what driver, login, etc. to use to connect to the SQL Server instance. Typically, the connection string will be stored in a configuration file somewhere within the application or web server.


3 Answers

The connection string you refer to:

<add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient" />

has nothing to do with Entity Framework. It's part of the template for all non-empty ASP.NET projects, both WebForms/WebPages and the MVC framework. It's used to connect to an embedded database that would reside in your App_Data folder and lets you use the ASP.NET provider model without needing an instance of SQL Server. If you weren't sure, take a look at the default connectionString value for the Membership section:

<membership>
  <providers>
    <clear/>
    <add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ApplicationServices"
         enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false"
         maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10"
         applicationName="/" />
  </providers>
</membership>
like image 57
Tieson T. Avatar answered Oct 19 '22 11:10

Tieson T.


You can easily see that it contains no application specific information

What application specific information do u expect from a connection string ?!

What is the real purpose of this key?

this is a new feature in Visual Studio 2012. Its called localdb. The mdf file always sits in the app_data folder of the project. It is introduced for the ease of developers, so that they don't have to worry about back-end while developing.

like image 32
Bilal Fazlani Avatar answered Oct 19 '22 12:10

Bilal Fazlani


The snippet show from Web or app.config is most likely the Connection string entry.

eg

<connectionStrings>
<add name="NameOfYourContext" connectionString="Data Source=SQLServerHost;Initial Catalog=The DBName;Integrated Security=True;MultipleActiveResultSets=True;App=EntityFramework"
   providerName="System.Data.SqlClient" />

This example if for SQL server yours SQL express. The difference highlights the intended use.

namely: The starting (http service) looks for web.config or WPF/Console/Forms applications look for an App.config.

Entity framework looks for a Connection string with the name of the context (see the constructor or your context). This resides

 <configuration>
      <connectionStrings>

by way of add element.

You can have as many connection strings as you like. EF and other processes can use the same connection string. It is not an artifact, indeed EF has reused a long established .net feature. eg http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.connectionstrings.aspx

Your context I assume is called ApplicationServices and this has caused the confusion. or the name is the default Connection Name used by service feature you are using such membership database. http://msdn.microsoft.com/en-us/library/bb547119%28v=vs.100%29.aspx eg You may have a section in the config, which has a "connectionStringName" of "ApplicationServices"

like image 38
phil soady Avatar answered Oct 19 '22 13:10

phil soady