ASP.NET MVC database
In order to learn ASP.NET MVC, we will build an Internet application.
Part 6: Adding database.
Creating a database
Visual Web Developer with a SQL database called SQL Server Compact free.
The database needed for this tutorial by following a few simple steps to create:
- Right-click the Solution Explorer window App_Data folder
- Select Add, New Item
- Select the SQL Server Compact Local Database *
- The database named Movies.sdf
- Click the Add button
* If none of the options SQL Server Compact Local Database, it means you have not installed SQL Server Compac on the computer. Please install the following link: the SQL Server Compact
Visual Web Developer will automatically create the database in the App_Data folder.
Note: In this tutorial, you are required to have some basic knowledge of SQL databases. If you want to study this topic, please visit our SQL tutorial .
Add a database table
Double-click the App_Data folder Movies.sdf files, Database Explorer window opens.
To create a new table in the database, right-click the Tables folder and select Create Table.
Create the following columns:
|ID||int (primary key)||No|
ID is an integer (whole number) identifies the table for each record.
Title is 100 characters long text column, the name of the film for storage.
Director is 100 characters long text columns for storing the director's name.
Date is the date columns for storing video release date.
After creating the above columns, you must set the ID column as the primary key for the table (record identifier). To do this, click on the column name (ID), and select Primary Key. In Column Properties window, set the Identity property to True:
Once you've created a table column, save the table and name MovieDBs.
We deliberately table named "MovieDBs" (ending in s). In the next chapter, you will see the data for the model "MovieDB". It looks strange, but this naming convention ensures that the controller connected to the database table, so you have to use.
Adding database records
You can use Visual Web Developer to add some test records to movie database.
Double-click the App_Data folder Movies.sdf file.
Right-click the Database Explorer window MovieDBs table and select Show Table Data.
Add some records:
|2||La Dolce Vita||Federico Fellini||01.01.1960|
NOTE: ID column will automatically update, you can not edit it.
Add Connection String
Add to your Web.config file <connectionStrings> element of the following elements: