essentially what I’m trying to do is get my head around Microsoft’s ASP.NET MVC 3.0. I’m using Visual Studio 2010 Ultimate. I followed the video tutorial at http://www.asp.net/mvc/videos/5-minute-introduction-to-aspnet-mvc which got me up and running and as a result have created a website which accesses a local database.
Question 1:
Does anyone know where that local database is? I believe it’s using a default SQLEXPRESS db somewhere. It doesn’t show up in Server Explorer, it’s not in the App_Data folder. I cant find it in any folder anywhere. It was – as I understand it – generated automatically using a model from the project and nowhere does it tell me where it’s put it.
Question 2:
What I’d really like to do is put the project online to play about with, and I’ve done that already by way of AppHarbor.com. They let you create a MS SQL Server database and give you a replacement connection string to bung in Web.config, but having replaced the connection string and rebuilt the solution – even after adding the AppHarbor database (I’m pretty sure pointlessly) to Server Explorer, my site still uses the elusive SQLEXPRESS database which I cant find.
The change I made in Web.config was from:
<connectionStrings>
<add name="ApplicationServices"
connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true"
providerName="System.Data.SqlClient" />
</connectionStrings>
To the format
<connectionStrings>
<add name="TestDB"
connectionString="Server=SERVER;Database=DBNAME;User ID=USERID;Password=DBPASSWORD;" />
</connectionStrings>
Question one would be nice to know, but it’s question 2 that’s really bothering me. What reference to the database have I missed that is causing it to use a database that I don’t want it to and know nothing about? I figure that there must be one, not least because when I remove the connection string completely from Web.config the thing still runs along fine.
Any help would be greatly appreciated.
So I finally worked out what the problem was – aided in no small part by everyone’s help here. So thank you. There were two main things I had wrong.
Firstly, turns out that the name of the connection string is important. I needed to name my connection string after the context I’d set up in MVC. So my “TestDB” was wrong, and instead for the video tutorial I linked to in my question the connection string should be called “SportsContext”.
So in the end, my connection string entry for Web.Config looked like this:
Which interestingly enough is not what they tell you that you should use on appharbor. Secondly, and another key point I was missing, while MVC will happily build your databases for you in SQLEXPRESS, it won’t do the same for SQL Server. And when building them yourself in SQL Server you have to be sure to get everything in correctly, including auto-incrementing ID columns and foreign keys etc.
Those two changes were all it took get my site up and working.