In this section, we will append some classes to manage movies in the database. These classes will become the "model" part of our MVC application.
We will use a. NET Framework data access technology called "Entity Framework" to define these model classes and use these classes for operations. Entity Framework (usually referred to as "Ef") supports a development example called "code-first. Code-first allows you to create model objects by writing some simple classes. You can access the database by accessing these classes. This is a very convenient and quick development mode.
4.1 install efcodefirst using nuget
We can use the nuget Package Manager (automatically installed when ASP. NET mvc3 is installed) to add the efcodefirst class library to our mvcmovie project. This class library allows us to directly use code-first. Click the "add library package reference" menu option under the "library Package Manager" sub-menu under the "Tools" menu, as shown in Figure 4-1.
Figure 4-1 use the nuget Package Manager
Click the "add library package reference" menu option. A dialog box is displayed, titled "add library package reference", 4-2.
Figure 4-2 "add library package reference" dialog box
By default, the "all" option on the left is selected. Because no package has been installed, "No items found" is displayed on the right panel ". Click the "online" option in the left-side Navigation Pane. The nuget Package Manager will retrieve all packages available on the server, as shown in Figure 4-3.
Figure 4-3 nuget Package Manager is Retrieving package information
There are hundreds of packages available on the server. Now we only focus on the efcodefirst package. Enter "efcode" in the search input box in the upper-right corner ". In the search results, select the efcodefirst package and click the Install button to install the installation package, as shown in Figure 4-4.
Figure 4-4 select and install the efcodefirst package
After you click the Install button, a accept license window is displayed, as shown in 4-5. In this window, you must click the "I accept" button to accept the license terms before the installation can continue.
Figure 4-5 accept a license window
After the installation is complete, click Close. The entityframework assembly is automatically loaded in our mvcmovie Project, which contains the efcodefirst class library.
Figure 4-6 entityframework assembly is automatically loaded after installation
4.2 Add model classes
In Solution Explorer, right-click the models folder and click "class" under the "add" menu, as shown in Figure 4-7.
Figure 4-7 Add a model class
Click the "class" menu, and the "Add new project" dialog box is displayed. In this dialog box, name the class name "movie", as shown in 4-8.
Figure 4-8 name the class in the "Add new project" dialog box
Click the Add button. In Solution Explorer, a movie. CS class definition file is added to the models folder and the file is open, as shown in Figure 4-9.
Figure 4-9 movie. CS class definition file added and open
Add the following five attributes to the movie. CS file.
Public class movie
{
Public int ID {Get; set ;}
Public String title {Get; set ;}
Public datetime releasedate {Get; set ;}
Public String genre {Get; set ;}
Public decimal price {Get; set ;}
}
We will use the movie class to represent the movie in the database ). An instance of each movie object corresponds to a row in the data table. Each attribute in the movie class is mapped to each column in the data table.
In the same movie. CS file, append the following moviedbcontext class.
Public class moviedbcontext: dbcontext
{
Public dbset <movie> movies {Get; set ;}
}
The moviedbcontext class represents the context object of the movie database in the Entity Framework, which is used to process data access and update. The moviedbcontext object inherits the dbcontext base class in the Entity Framework. To reference the dbcontext class, you need to append the following using statement to the header of the movie. CS file.
Using system. Data. entity;
The complete code in the movie. CS file is shown in the code list 4-1.
Code list 4-1 complete movie. CS File
Using system;
Using system. Data. entity;
Namespace mvcmovie. Models
{
Public class movie
{
Public int ID {Get; set ;}
Public String title {Get; set ;}
Public datetime releasedate {Get; set ;}
Public String genre {Get; set ;}
Public decimal price {Get; set ;}
}
Public class moviedbcontext: dbcontext
{
Public dbset <movie> movies {Get; set ;}
}
}
To access data from a database, code similar to the above must be written. In the next section, we will create a new moviescontroller class to display data in the database and allow users to create a new movie (movie) list.