Adding a Model
[!NOTE] An updated version of this tutorial is available here that uses ASP.NET MVC 5 and Visual Studio 2013. It’s more secure, much simpler to follow and demonstrates more features.
In this section you’ll add some classes for managing movies in a database. These classes will be the “model” part of the ASP.NET MVC application.
You’ll use a .NET Framework data-access technology known as the Entity Framework to define and work with these model classes. The Entity Framework (often referred to as EF) supports a development paradigm called Code First. Code First allows you to create model objects by writing simple classes. (These are also known as POCO classes, from “plain-old CLR objects.”) You can then have the database created on the fly from your classes, which enables a very clean and rapid development workflow.
Adding Model Classes
In Solution Explorer, right click the Models folder, select Add, and then select Class.
Enter the class name “Movie”.
Add the following five properties to the Movie
class:
[!code-csharpMain]
1: public class Movie
2: {
3: public int ID { get; set; }
4: public string Title { get; set; }
5: public DateTime ReleaseDate { get; set; }
6: public string Genre { get; set; }
7: public decimal Price { get; set; }
8: }
We’ll use the Movie
class to represent movies in a database. Each instance of a Movie
object will correspond to a row within a database table, and each property of the Movie
class will map to a column in the table.
In the same file, add the following MovieDBContext
class:
[!code-csharpMain]
1: public class MovieDBContext : DbContext
2: {
3: public DbSet<Movie> Movies { get; set; }
4: }
The MovieDBContext
class represents the Entity Framework movie database context, which handles fetching, storing, and updating Movie
class instances in a database. The MovieDBContext
derives from the DbContext
base class provided by the Entity Framework.
In order to be able to reference DbContext
and DbSet
, you need to add the following using
statement at the top of the file:
[!code-csharpMain]
1: using System.Data.Entity;
The complete Movie.cs file is shown below. (Several using statements that are not needed have been removed.)
[!code-csharpMain]
1: using System;
2: using System.Data.Entity;
3:
4: namespace MvcMovie.Models
5: {
6: public class Movie
7: {
8: public int ID { get; set; }
9: public string Title { get; set; }
10: public DateTime ReleaseDate { get; set; }
11: public string Genre { get; set; }
12: public decimal Price { get; set; }
13: }
14:
15: public class MovieDBContext : DbContext
16: {
17: public DbSet<Movie> Movies { get; set; }
18: }
19: }
Creating a Connection String and Working with SQL Server LocalDB
The MovieDBContext
class you created handles the task of connecting to the database and mapping Movie
objects to database records. One question you might ask, though, is how to specify which database it will connect to. You’ll do that by adding connection information in the Web.config file of the application.
Open the application root Web.config file. (Not the Web.config file in the Views folder.) Open the Web.config file outlined in red.
Add the following connection string to the <connectionStrings>
element in the Web.config file.
[!code-xmlMain]
1: <add name="MovieDBContext"
2: connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Movies.mdf;Integrated Security=True"
3: providerName="System.Data.SqlClient"
4: />
The following example shows a portion of the Web.config file with the new connection string added:
[!code-xmlMain]
1: <connectionStrings>
2: <add name="DefaultConnection"
3: connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-MvcMovie-2012213181139;Integrated Security=true"
4: providerName="System.Data.SqlClient"
5: />
6: <add name="MovieDBContext"
7: connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Movies.mdf;Integrated Security=True"
8: providerName="System.Data.SqlClient"
9: />
10: </connectionStrings>
This small amount of code and XML is everything you need to write in order to represent and store the movie data in a database.
Next, you’ll build a new MoviesController
class that you can use to display the movie data and allow users to create new movie listings.
|