ASP.NET MVC

Web.Config: Database Connections

Whenever databases are used with web pages, a connection to the database is required. In both MVC and Web Forms it is typical to connect the database using a Connection String stored in an XML file called Web.Config in the root directory (there are other web.config files other directories)

Microsoft Membership System

Most web sites need to authenticate users so that their personal data can only be accessed by the user who owns the data, and administrators of the web site. Both Web Forms and MVC have an inbuilt system to manage user names and passwords called the Microsoft Membership system. To use this you need a connection string called DefaultConnection (see below). The system changed in MVC5, and previous systems are now obsolete!

Automatic Database Creation

Thus in most web sites there is a need for two databases the membership database and the database to support the main functions of the web site. Although it is possible to combine the tables from both databases into one database, it is easier to keep them separate whilst development is taking place. The membership database is created when the first user registers a user name and password, the web site database will be created as soon as any web page is accessed that uses data from that database. Connection strings are automatically inserted into the Web.Config file.

Connection Strings

There are three variations on SQL Server database files. There is a compact version of SQL Server but it does not work well when the database is dropped and re-created so do not use this database for MVC. There is another referrred to as (LocalDb) v11.0 which is built in to Visual Studio since 2012 and this works better than any other. Click on the links below in order to be able to copy the connection string and paste it into your web.config file.

Change the name TalkIsCheap to the name of your current project

Coonection String Example

With the connection strings in place run your program and register yourself and your project partner as users of the system. You can then add new controllers and CRUD web pages to your project as outlined below in Controllers & Views. When you access those web pages for the first time the project database will be created automatically! To see the database(s) set Show All Files, and then select the database and right click to include in the project.

Updating the Project Database

In order for changes to the Models classes to be reflected in the project database you need to add one line of code to the Global.aspx file. The line of code can be copied from your DatabaseContext.cs class in the models folder. It is last line in the block comments. Copy it to Global.aspx and remove the comment slashes.

Global.aspx

With that line in place very time the model classes are modifed the database is dropped and re-created. ALL DATA IS LOST!!! I will show you how to re-populate the database using code in a later web page.

If you wish to update the database without loosing data, use data migrations instead.

MVC Index