How to Create a Database for a Movie Database Application in 20 Minutes
Learn how to create a database for a movie database application in just about 20 minutes using ASP.NET MVC (C#).
Join the DZone community and get the full member experience.
Join For FreeWe are living in the 21st century — and people in the 21st century are consuming content online in video format. Online video consumption on a daily basis is reaching a new high. We should also take into consideration that a lot of companies and production houses have started their own movie database applications. The purpose of this article is to show you how to create a database for a movie database application in about 20 minutes using ASP.NET MVC (C#).
Overview of the Movie Database App
You should make use of the scaffolding features of Visual Studio 2008 and let it carry out the task of initial code generation, as well as content for our controllers, models, and views.
Since our objective is to keep everything simple, our focus will remain on the development of a simple movie database app that will allow us to list all movie database-related records, create new records related to the movie database, and edit the records of the movie database. For this purpose, make sure you develop an ASP.NET modular view-based website project and create the database and database model. More importantly, you should create the ASP.NET MVC Controller and ASP.NET MVC views.
Tip: The focus must be on taking advantage of the minimum number of features of the ASP.NET MVC framework required for the creation of our movie database application.
Having Visual Studio 2008 or Visual Web Developer 2008 Express is important to be able to develop a movie database application using ASP.NET MVC (C#). You will also need to download the ASP.NET MVC framework.
Tip: If you are using Visual Web Developer Express 2008, make sure you get Service Pack 1 installed in advance. Once you are done installing Visual Studio 2008 or Visual Studio 2008 Express, do not forget to install the ASP.NET MVC framework.
Step 1: Let’s Start the Project
Make sure you create a new ASP.NET MVC web application project using Visual Studio 2008. You need to select the File option from the menu and click on the New Project option. Now, choose C# as the language of programming. Also, remember to choose the ASP.NET MVC web application project template. Finally, name your project MovieApplication and click the OK button.
Don’t forget that you should choose .NET framework 3.5 from the dropdown list given at the top of the new project dialogue. If you don’t, the ASP.NET MVC web application project template will not appear. You will also be asked to create a unit test project by Visual Studio. However, we are not going to create any unit tests because of time constraints. Choose the option No and click the OK button on your screen.
An ASP.NET MVC web application always has a standard set of folders accessible in the Solution Explorer window. These are Models, Views, and Controller folders that will require you to add files to them for creating a movie database application.
And always remember to delete the following files and folders in this process:
- Controllers\HomeController.cs
- Views\Home
Step 2: Creating a Database
You will need a database to help your movie application to be able to hold movie records. Visual Studio offers the luxury of a free database, SQL Server Express. Follow the steps given in the next paragraph to create your database.
First of all, you should right-click the App_Data folder in the Solution Explorer window. From there, you should click on the menu option Add, New Item. Secondly, choose the data category and SQL Server Database template. Finally, name it MoviesDB.mdf and click the Add Button.
Tip: Once you are done creating the database, you are all set to connect to the database by double-clicking MoviesDB.mdf. You can locate this file in the App_Data folder. In this way, the Server Explorer window gets opened.
Step 3: Creating a New Table Database
This is our next requirement to be able to create the table for your movie application database. Right-click the Tables folder with your mouse and choose the menu option Add a New Table. The selection of this option opens the database table designer. Now, you should create the following database columns:
Column Name |
Data Type |
Allow Nulls |
Id |
Int |
False |
Title |
Nvarchar(100) |
False |
Director |
Nvarchar(100) |
False |
DateReleased |
DateTime |
False |
The first column contains two very special properties. You must mark the Id Column as the PrimaryKey Column and click on the Primary Key button. In addition to this, remember to mark the Id column as an identity column, scroll down to identify the specific section, expand it, and change the property of the identity to Yes. Finally, hit Save to save the table as Movies.
Now, your table for your movie database application is ready. Once you are done creating the table, you need to add some movie records to it. Right-click the Movies table in the Server Explorer window and select the menu option called Show Data Table to be able to enter the list of the movies you like the most.
The database for your movie database application is now ready!
Opinions expressed by DZone contributors are their own.
Comments