In this section, you will add classes that are used to manage movies in the database. These classes are "model" in the ASP.net MVC application.
You will use the. NET Framework Data Access Technology Entity Framework to define and use these model classes. The Entity Framework (commonly called EF) is a development model that supports code first. Code first allows you to create an object model by writing a simple class. (as opposed to "original CLR Objects", which is also called the Poco Class), you can then create a database from your class, which is a very clean and fast development workflow.
Add Model Class
In Solution Explorer, right-click the Model folder, select Add, and then select a class.
Enter the Class name "Movie".
Add the following five properties to the movie class:
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'll use the movie class to represent the movies in the database. Each instance of the movie object will correspond to one row of the database table, and each property of the movie class will correspond to a column of the table.
In the same file, add the following Moviedbcontext class:
public class Moviedbcontext:dbcontext
{public
dbset<movie> Movies {get; set;}
}
The Moviedbcontext class represents the Entity Framework's movie Database class, which is responsible for fetching, storing, updating, and processing instances of Movie classes in the database. Moviedbcontext inherits the DbContext base class from the Entity Framework.
To be able to reference dbcontext and dbset, you need to add the following using statement at the top of the file:
Using System.Data.Entity;
The complete Movie.cs file is shown below. (some unused using statements have been deleted)
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;}
}
}
Create a connection string and use SQL Server LocalDB
The Moviedbcontext class you just created is used to connect to the database and map the movie object to database table records. You may ask a question, how to specify that it will be connected to that database. Specify the connection to that database by adding the database connection information to the application's web.config file.
Open the web.config file in the application's root directory. (Not a web.config file under the View folder.) To open the red highlighted web.config file.
Add the following connection string to <connectionStrings> within the web.config file.
<add name= "Moviedbcontext"
connectionstring= "Data source= (LocalDB) \v11.0; attachdbfilename=| datadirectory|\movies.mdf;integrated security=true "
providername=" System.Data.SqlClient "
The following example shows the newly added connection string in a partial Web.config file:
<connectionStrings>
<add name= "defaultconnection"
connectionstring= "Data source= (LocalDb) \v11.0 ; Initial catalog=aspnet-mvcmovie-2012213181139;integrated security=true "
providername=" System.Data.SqlClient "
/>
<add name=" Moviedbcontext "
connectionstring=" Data source= (LocalDB) \v11.0; attachdbfilename=| datadirectory|\movies.mdf;integrated security=true "
providername=" System.Data.SqlClient "
/>
< /connectionstrings>
In order to represent and store movie data into a database, the small amount of code and XML is all you need.
Next, you will create a new Moviescontroller class that you can use to Displays the movie data and allows the user to create a new movie list.