This is about ConnectionStrings / ASP.NET MVC with Visual Studio 2012 ultimate & SQL Server Express 2012.
Following up with this tutorial here: http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/intro-to-aspnet-mvc-4 I came across an issue with these two connection strings at my web.config:
<connectionStrings>
<add name="DefaultConnection"
connectionString="Data Source=(LocalDb)\v11.0;
Initial Catalog=aspnet-MvcMovie-users;
Integrated Security=SSPI;
AttachDBFilename=|DataDirectory|\aspnet-MvcMovie-users.mdf"
providerName="System.Data.SqlClient" />
<add name="MovieDBContext"
connectionString="Data Source=(LocalDB)\v11.0;
AttachDbFilename=|DataDirectory|\Movies.mdf;
Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
the website works fine but I couldn't fingure out why the first db is created in the App_Data folder while the second one is created in "C:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXPRESS\MSSQL\DATA"?! I supposed that both will be created in App_data because both utilize this attribute: AttachDBFilename=|DataDirectory|!
note: the tutorial mentions that it should be in the App_Data & they added a screenshot that shows it there indeed!
I have been looking for an answer and got into the complicity of SQL (I thought User Instances might be the solution) but couldn't reach an answer for this : |
(this might be useful to read about User Instances http://msdn.microsoft.com/en-us/library/bb264564(v=sql.90).aspx)
Any ideas are greatly appreciated. Thanks in advance.
Regards
Using AttachDBFileName and User Instance means that SQL Server is creating a special copy of that database file for use by your program. If you have two different programs using that same connection string, they get two entirely different copies of the database.
Connection string holds information that is required to communicate with the database. You can use connection string with any database like MSSQL, MySQL, MS Access etc. Some of the main parameters present in Asp . Net connection string are: 1) “name”: Name of the connection stirng.
|DataDirectory| (enclosed in pipe symbols) is a substitution string that indicates the path to the database. It eliminates the need to hard-code the full path which leads to several problems as the full path to the database could be serialized in different places.
To read the connection string into your code, use the ConfigurationManager class. string connStr = ConfigurationManager. ConnectionStrings["myConnectionString"].
after research/tests it turned out to be as follows:
VS will look at the class name of the DataContext and will look to see if you have provided a connection string with the same name as the class name; for example:
public class MovieDataContext : DbContext
and
<connectionStrings><add name="MovieDataContext" ...
if it manages to find a matching connection string it will create the DB based on the criteria you specified in the respective data string (to add the DB to the App_Data set the path of the DB to |DataDirectory| as shown in both connection strings mentioned in the question); if the name doesn't match or you didn't provide any connection string, VS will fall back to the default settings and will create the DB in the default location/settings (usually C:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXPRESS\MSSQL\DATA).
note neither the "Integrated Security" settings nor the "Initial Catalog" play any role with this (I was able to create the DB in the App_Data with both Integrated Security = True & Integrated Security = SSPI and with/without Initial Catalog).
Hope this helps. Thanks for everyone that participated.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With